LINQ连接两个表 [英] LINQ joining two tables
问题描述
我有两个表说A和B
一COLS是GUID,someintVar,someMoreIntvar $ B $条b B山坳是GUID,someItemNO,SomeItemDesc
现在一个GUID我将有只有一行在表A,但我可以有相同的GUID多行。
现在我希望基于GUID,并选择值的一类查询数据库。
这个类将有将举行第二个表来不同行的列表。
我该怎么办呢?
现在,我基于多少行为GUID第二个表在那里得到的结果,许多项目。
VAR itemColl =从p在db.A
在db.B加盟项目上p.CardID等于项目。 CardId中
其中p.CardID ==一些GUID
选择新的
{
p.CardID,
p.secondCol,
p.ThirdCol,
item.ItemNo //如何将它们添加集合或列表英寸
};
Unested,但如何重新写了一点
$ b在db.A
其中p.CardID ==一些GUID
VAR itemColl =由对$ b选择新{
p.CardID,
p.secondCol,
p.ThirdCol,
项= db.B.Where(b = GT; b.CardID == p.CardID)
//.Select(b=>b.ItemNo)[见评论]
}
另外,你也许可以组...
I have two tables say A and B. A cols are GUID, someintVar, someMoreIntvar B col are GUID, someItemNO, SomeItemDesc
Now for one GUID I will have only one row in Table A. But I can have multiple rows for the same GUID. Now I want to query the database based on GUID and select values in a class. This class will have a list that will hold different rows coming from the second table. How can I do it?
Right Now I am getting many items in the result based on how many rows are there in the second table for that GUID.
var itemColl = from p in db.A
join item in db.B on p.CardID equals item.CardID
where p.CardID == "some GUID"
select new
{
p.CardID,
p.secondCol,
p.ThirdCol,
item.ItemNo // How to add them in a collection or list.
};
Unested, but how about re-writing it a bit:
var itemColl = from p in db.A
where p.CardID == "some GUID"
select new {
p.CardID,
p.secondCol,
p.ThirdCol,
Items = db.B.Where(b=>b.CardID==p.CardID)
//.Select(b=>b.ItemNo) [see comments]
}
Alternatively, you could perhaps group...
这篇关于LINQ连接两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!