LINQ to SQL:多个列上的多个联接。这可能吗?
给定:
名为 TABLE_1
的表,其中包含以下列:
ID
ColumnA
ColumnB
- < code>ColumnC
我有 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.
问题:
我需要在 LINQ 中重写该查询。我尝试过尝试一下:
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 :(
How do I write my query in LINQ?我做错了什么?
Given:
A table named TABLE_1
with the following columns:
ID
ColumnA
ColumnB
ColumnC
I have SQL query where TABLE_1
joins on itself twice based off of ColumnA
, ColumnB
, ColumnC
. The query might look something like this:
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.
Problem:
I need that Query to be rewritten in LINQ. I've tried taking a stab at it:
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 :(
How do I write my query in LINQ? What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在 Linq to SQL 中连接多个列有点不同。
您必须利用匿名类型并为您想要比较的多个列编写一个类型。
乍一看这似乎令人困惑,但一旦您熟悉了 SQL 由表达式组成的方式,它就会变得更有意义,在幕后这将生成您正在寻找的联接类型。
编辑添加基于评论的第二次加入的示例。
Joining on multiple columns in Linq to SQL is a little different.
You have to take advantage of anonymous types and compose a type for the multiple columns you wish to compare against.
This seems confusing at first but once you get acquainted with the way the SQL is composed from the expressions it will make a lot more sense, under the covers this will generate the type of join you are looking for.
EDIT Adding example for second join based on comment.
你还可以使用:
U can also use :
在 LINQ2SQL 中,使用内连接时很少需要显式连接。
如果您的数据库中有正确的外键关系,您将自动在 LINQ 设计器中获得一个关系(如果没有,您可以在设计器中手动创建一个关系,尽管您的数据库中确实应该有正确的关系)
然后您可以使用“点符号”访问相关表,
将生成查询
在我看来,这更具可读性,让您专注于您的特殊条件,而不是连接的实际机制。
编辑
这当然只适用于你想加入与我们的数据库模型一致的情况。如果您想加入“模型外部”,则需要采用手动加入,如 来自 Quintin Robinson 的答案
In LINQ2SQL you seldom need to join explicitly when using inner joins.
If you have proper foreign key relationships in your database you will automatically get a relation in the LINQ designer (if not you can create a relation manually in the designer, although you should really have proper relations in your database)
Then you can just access related tables with the "dot-notation"
will generate the query
In my opinion this is much more readable and lets you concentrate on your special conditions and not the actual mechanics of the join.
Edit
This is of course only applicable when you want to join in the line with our database model. If you want to join "outside the model" you need to resort to manual joins as in the answer from Quintin Robinson
Title_Authors 是一次查找两件事加入项目结果并继续链接
Title_Authors is a look up two things join at a time project results and continue chaining
我想给出另一个使用多个 (3) 连接的示例。
I would like to give another example in which multiple (3) joins are used.
您可以使用 LINQ 方法语法来联接多个列。这是一个例子,
注意:编译器在编译时将查询语法转换为方法语法。
You can use LINQ Method Syntax to join on multiple columns. It's an example here,
Note: The compiler converts query syntax into method syntax at compile time.
如果两个表中的列数不同,您也可以加入,并且可以将静态值映射到表列
You can also join if the number of columns are not same in both tables and can map static value to table column
A 和 B 别名必须与 e 表和 t 表中的 Hrco 和位置代码 - “equal new”过滤器中的 Hrco 和位置代码组合一致。这将节省您的时间,因为我不断收到“不在左侧范围内”编译错误,因为我认为过滤器是 e.Hrco、t.Hrco 配对的过滤器。
The A and B alias must line up with Hrco and Position code from e table and t table - Hrco and Position Code combinations in the "equal new" filter. This will save you time because I kept getting "Not in scope on the left side" compile errors because I thought the filter was e.Hrco, t.Hrco pairing for the filter.