LINQ中的简单内部联接 [英] Simple inner join in linq
本文介绍了LINQ中的简单内部联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个表A
,B
和C
. C
有许多B
,其中有许多A
.
I have three tables A
, B
and C
. C
has many B
's which has many A
's.
我想将所有这些数据显示在一棵树中,所以我将db.A
绑定到了具有三层层次结构的嵌套控件.唯一的问题是我得到的行太多,因为它没有在B
和C
之间进行内部联接.
I want to display all this data in a tree so I bound db.A
to my nested control which had a three layer hierarchy. The only problem is I get too many rows because it's not doing an inner join between B
and C
.
那么我如何在linq中做这样的事情:
So how can I do something like this in linq:
SELECT A.name, B.name
FROM A
INNER JOIN B ON A.id = B.AID
INNER JOIN C ON B.id = C.BID
GROUP BY A.Name, B.Name
ORDER BY A.Name
我已经尝试了以下方法:
I've tried the following to no avail:
from a in A
join b in B on a.id equals b.AID
join c in C on b.id equals c.BID
select c
非常感谢
推荐答案
尝试一下:
from a in A
join b in B on a.id equals b.AID
join c in C on b.id equals c.BID
group new {a,b} by new {a.name,b.name} into g
select g
以上联接与您在sql查询中所做的相同,但您想要的是:
Above join is the same as you did in sql query but what you want is :
from a in A
join b in B on a.id equals b.AID
join c in C on b.id equals c.BID
group c by c.name into g
select g
这篇关于LINQ中的简单内部联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文