aar*_*ona 118 c# left-join multiple-columns linq-to-sql
鉴于:
以TABLE_1下列列命名的表:
IDColumnAColumnBColumnC我有SQL查询,其中TABLE_1对自己加入基于两次关闭的ColumnA,ColumnB,ColumnC.查询可能如下所示:
Select t1.ID, t2.ID, t3.ID
From TABLE_1 t1
Left Join TABLE_1 t2 On
t1.ColumnA = t2.ColumnA
And t1.ColumnB = t2.ColumnB
And t1.ColumnC = t2.ColumnC
Left Join TABLE_1 t3 On
t2.ColumnA = t3.ColumnA
And t2.ColumnB = t3.ColumnB
And t2.ColumnC = t3.ColumnC
... and query continues on etc.
Run Code Online (Sandbox Code Playgroud)
问题:
我需要在LINQ中重写Query.我试过去刺它:
var query =
from t1 in myTABLE1List // List<TABLE_1>
join t2 in myTABLE1List
on t1.ColumnA equals t2.ColumnA
&& t1.ColumnB equals t2.ColumnA
// ... and at this point intellisense is making it very obvious
// I am doing something wrong :(
Run Code Online (Sandbox Code Playgroud)
如何在LINQ中编写查询?我究竟做错了什么?
Qui*_*son 225
加入Linq to SQL中的多个列有点不同.
var query =
from t1 in myTABLE1List // List<TABLE_1>
join t2 in myTABLE1List
on new { t1.ColumnA, t1.ColumnB } equals new { t2.ColumnA, t2.ColumnB }
...
Run Code Online (Sandbox Code Playgroud)
您必须利用匿名类型并为要比较的多个列组成一个类型.
这一开始似乎令人困惑,但是一旦你熟悉了SQL从表达式组成的方式它会更有意义,在封面下这将生成你正在寻找的连接类型.
编辑根据注释添加第二个连接的示例.
var query =
from t1 in myTABLE1List // List<TABLE_1>
join t2 in myTABLE1List
on new { A = t1.ColumnA, B = t1.ColumnB } equals new { A = t2.ColumnA, B = t2.ColumnB }
join t3 in myTABLE1List
on new { A = t2.ColumnA, B = t2.ColumnB } equals new { A = t3.ColumnA, B = t3.ColumnB }
...
Run Code Online (Sandbox Code Playgroud)
Alb*_*nbo 10
在LINQ2SQL中,在使用内部联接时很少需要显式连接.
如果数据库中有正确的外键关系,您将自动在LINQ设计器中获得关系(如果不是,您可以在设计器中手动创建关系,尽管您应该在数据库中确实存在正确的关系)

然后您可以使用"点符号"访问相关表
var q = from child in context.Childs
where child.Parent.col2 == 4
select new
{
childCol1 = child.col1,
parentCol1 = child.Parent.col1,
};
Run Code Online (Sandbox Code Playgroud)
将生成查询
SELECT [t0].[col1] AS [childCol1], [t1].[col1] AS [parentCol1]
FROM [dbo].[Child] AS [t0]
INNER JOIN [dbo].[Parent] AS [t1] ON ([t1].[col1] = [t0].[col1]) AND ([t1].[col2] = [t0].[col2])
WHERE [t1].[col2] = @p0
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [4]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1
Run Code Online (Sandbox Code Playgroud)
在我看来,这更具可读性,让你专注于你的特殊条件而不是连接的实际机制.
编辑
当然,只有当您想要加入我们的数据库模型时才适用.如果你想加入"模型之外"你需要求助于手动加入作为答案从昆廷·罗宾逊
小智 10
Title_Authors是一个查找两件事情,一次加入项目结果并继续链接
DataClasses1DataContext db = new DataClasses1DataContext();
var queryresults = from a in db.Authors
join ba in db.Title_Authors
on a.Au_ID equals ba.Au_ID into idAuthor
from c in idAuthor
join t in db.Titles
on c.ISBN equals t.ISBN
select new { Author = a.Author1,Title= t.Title1 };
foreach (var item in queryresults)
{
MessageBox.Show(item.Author);
MessageBox.Show(item.Title);
return;
}
Run Code Online (Sandbox Code Playgroud)
你还可以使用:
var query =
from t1 in myTABLE1List
join t2 in myTABLE1List
on new { ColA=t1.ColumnA, ColB=t1.ColumnB } equals new { ColA=t2.ColumnA, ColB=t2.ColumnB }
join t3 in myTABLE1List
on new {ColC=t2.ColumnA, ColD=t2.ColumnB } equals new { ColC=t3.ColumnA, ColD=t3.ColumnB }
Run Code Online (Sandbox Code Playgroud)