C# 关于三表linq查询的帮助

C# 关于三表linq查询的帮助,c#,linq-to-sql,C#,Linq To Sql,我需要使用3个表: 品牌- 布兰迪 名称 BrandSource- 布兰迪 SourceID 来源- SourceID SourceName 图像 因此,我与BrandSource有很多关系,BrandSource是我的中间表。我将每个品牌列显示在一个表中,并为源图像创建了一个新列。基本上,如果一个品牌有5个来源,我需要它显示该品牌的一行以及我制作的新列中的5个不同来源图像(一个单元格中有5个图像) 由于我加入了这三个表,它明显地看到BrandSource表中有5行,并且在一个单元格中显示了每个

我需要使用3个表:
品牌-
布兰迪
名称

BrandSource-
布兰迪
SourceID

来源-
SourceID
SourceName
图像

因此,我与BrandSource有很多关系,BrandSource是我的中间表。我将每个品牌列显示在一个表中,并为源图像创建了一个新列。基本上,如果一个品牌有5个来源,我需要它显示该品牌的一行以及我制作的新列中的5个不同来源图像(一个单元格中有5个图像)

由于我加入了这三个表,它明显地看到BrandSource表中有5行,并且在一个单元格中显示了每个品牌的5行以及单个源图像

我相信我可以选择不同的品牌,但这仍然不能解决我的问题:如何让每个品牌的所有源图像显示在同一个单元格中

这是我的linq代码:(正如你所看到的,为了简洁起见,我在上面遗漏了一些信息)

var join=来自db.Brands中的b
在b.BrandID等于b.BrandID的db.Brands中加入bs
将sb加入db.SourceID上的db.Sources等于sb.SourceID
选择新的{Brand=b,source=sb.Image,c=b.Description.Length<204?b.Description:b.Description.Substring(0204)+“…”};
下面是我如何使用它:

foreach (var result in join)
    {
        bool a = result.Brand.Active;
        string chk = string.Empty;
        if (a == true)
            chk = "checked='checked'";
        else
            chk = "";

            resultSpan.InnerHtml += "<tr><td><input type='checkbox' " + chk + "></td><td width='1%'><img width='50px' src='" + result.Brand.Image + "'</img></td>" +
                "<td>" + result.Brand.Name + "</td><td width='60%'>" + result.c + "</td><td><img src='"+result.source+"'></img></td><td>" + result.Brand.DateCreated + "</td><td>" + result.Brand.DateModified + "</td></tr>";

    }
foreach(变量结果为join)
{
bool a=result.Brand.Active;
string chk=string.Empty;
如果(a==true)
chk=“checked='checked'”;
其他的
chk=“”;
resultSpan.InnerHtml+=“”+
“+result.Brand.Name+”“+result.c+”“+result.Brand.DateCreated+”“+result.Brand.DateModified+”;
}

按BrandID对LINQ查询排序 然后使用一个变量来跟踪它是否是一个新品牌

 int lastBrandID = 0;
 string closeHtml = "";
 foreach (var result in join)
    {
        if(result.Brand.BrandID != lastBrandID)
        {
            resultSpan.InnerHtml += closeHtml;
            bool a = result.Brand.Active;
            string chk = string.Empty;
            if (a == true)
                chk = "checked='checked'";
            else
                chk = "";

            resultSpan.InnerHtml += "<tr><td><input type='checkbox' " + chk + "></td><td width='1%'><img width='50px' src='" + result.Brand.Image + "'</img></td>" +
                "<td>" + result.Brand.Name + "</td><td width='60%'>" + result.c + "</td><td>";
            closeHtml = "</td><td>" + result.Brand.DateCreated + "</td><td>" + result.Brand.DateModified + "</td></tr>";
            lastBrandID = result.Brand.BrandID;
        }
        resultSpan.InnerHtml += "<img src='"+result.source+"'></img>";
    }
    resultSpan.InnerHtml += closeHtml;
int lastBrandID=0;
字符串closeHtml=“”;
foreach(联接中的var结果)
{
if(result.Brand.BrandID!=lastBrandID)
{
resultSpan.InnerHtml+=closeHtml;
bool a=result.Brand.Active;
string chk=string.Empty;
如果(a==true)
chk=“checked='checked'”;
其他的
chk=“”;
resultSpan.InnerHtml+=“”+
“+result.Brand.Name+”+result.c+”;
closeHtml=“”+result.Brand.DateCreated+”+result.Brand.DateModified+”;
lastBrandID=result.Brand.BrandID;
}
resultSpan.InnerHtml+=“”;
}
resultSpan.InnerHtml+=closeHtml;
在旁注中,使用StringBuilder而不是连接字符串。

你有了一个好的开始,但我认为你最好不要自己做三联。LINQtoSQL可以为您处理这些细节。如果您暂时离开查询方面,从所需的结果开始,您会做得更好。据我所知,你想要的对象类型是一个品牌列表,每个品牌都应该包含一个来源列表。以下是您如何做到这一点(从下载开始)

LinqPad显示这在一个查询中执行,但是在结果对象中组装
列表的勇气发生在幕后。以下是LinqPad执行的操作:

SELECT [t0].[BrandID], [t0].[Name], [t1].[SourceID], [t1].[SourceName], [t1].[Image], (
    SELECT COUNT(*)
    FROM [Source] AS [t3]
    INNER JOIN [BrandSource] AS [t4] ON [t3].[SourceID] = [t4].[SourceID]
    WHERE [t4].[BrandID] = [t0].[BrandID]
    ) AS [value]
FROM [Brand] AS [t0]
LEFT OUTER JOIN ([Source] AS [t1]
    INNER JOIN [BrandSource] AS [t2] ON [t1].[SourceID] = [t2].[SourceID]) ON [t2].[BrandID] = [t0].[BrandID]
以下是一些测试数据,供以下人员在家中使用:

create table Brand (
BrandID int,
Name varchar(50),
)

create table BrandSource (
BrandID int,
SourceID int
)

create table Source (
SourceID int,
SourceName varchar(50),
[Image] varchar(50)
)

insert into Brand select 1, 'Brand1'
insert into Brand select 2, 'Brand2'
insert into Brand select 3, 'Brand3'

insert into Source select 1, 'Source1', 'src1.gif'
insert into Source select 2, 'Source2', 'src2.jpg'
insert into Source select 3, 'Source3', 'src3.bmp'
insert into Source select 4, 'Source4', 'src4.png'
insert into Source select 5, 'Source5', 'src5.raw'

insert into BrandSource select 1, 1
insert into BrandSource select 1, 2
insert into BrandSource select 1, 3
insert into BrandSource select 2, 2
insert into BrandSource select 2, 4

select * from Brand
select * from BrandSource
select * from Source
请注意,通过这种方式,您会得到品牌#3的一个空源列表,我想这是您想要的。您的原始查询
内部连接
ed Brand#3远离

最后,下面是一个如何使用查询结果的示例:

foreach (var result in results) {
   string chk = (result.Brand.Active ? " checked='checked'" : "");
   var buf = new StringBuilder();
   buf.Append("<tr>");
   buf.AppendFormat("<td><input type='checkbox'{0}></td>", chk);
   buf.AppendFormat("<td width='1%'><img width='50px' src='{0}'></img></td>", result.Brand.Image);
   buf.AppendFormat("<td>{0}</td>", result.Brand.Name);
   buf.Append("<td>");

   foreach(var src in result.Sources) {
      buf.AppendFormat("<img src='{0}'></img>", src.Image);
   }

   buf.Append("</td>");
   buf.Append("</tr>");

   resultSpan.InnerHtml = buf.ToString();

}
foreach(结果中的var结果){
字符串chk=(result.Brand.Active?“checked='checked'”:);
var buf=新的StringBuilder();
buf.追加(“”);
基本单位。附录格式(“,chk);
buf.AppendFormat(“,result.Brand.Image”);
buf.AppendFormat(“{0}”,result.Brand.Name);
buf.追加(“”);
foreach(result.Sources中的var src){
buf.AppendFormat(“,src.Image”);
}
buf.追加(“”);
buf.追加(“”);
resultSpan.InnerHtml=buf.ToString();
}

您可以在原始查询中使用
GroupBy
进行此操作(或在查询中使用
group..by..into
),因此:

var groups = join.GroupBy(b => b.Brand);

foreach (var group in groups)
{
    var brand = group.Key;
    foreach (var row in group)
    {
       // you get the idea
    }

}

这正是我想要的。非常感谢。
foreach (var result in results) {
   string chk = (result.Brand.Active ? " checked='checked'" : "");
   var buf = new StringBuilder();
   buf.Append("<tr>");
   buf.AppendFormat("<td><input type='checkbox'{0}></td>", chk);
   buf.AppendFormat("<td width='1%'><img width='50px' src='{0}'></img></td>", result.Brand.Image);
   buf.AppendFormat("<td>{0}</td>", result.Brand.Name);
   buf.Append("<td>");

   foreach(var src in result.Sources) {
      buf.AppendFormat("<img src='{0}'></img>", src.Image);
   }

   buf.Append("</td>");
   buf.Append("</tr>");

   resultSpan.InnerHtml = buf.ToString();

}
var groups = join.GroupBy(b => b.Brand);

foreach (var group in groups)
{
    var brand = group.Key;
    foreach (var row in group)
    {
       // you get the idea
    }

}