[英]Linq joining tables
我有以下兩個表
T1
==========
tid pId bId desc
1 11 10 xyz
2 9 7 abc
3 11 9 tyy
和
T2
=========
id bId exp
1 10 main
2 9 front
3 10 front
4 8 top
5 10 main2
6 9 newM
我的模特班是
public class test
{
public int tid {get; set;}|
public int bid {get; set:}
public IEnumerable<T2> t2 {get; set;}
}
我想編寫linq查詢,以便可以選擇測試類的列表
var records=(from c in T1
join y in T2 on c.bid equals y.bid
where c.id==id
select {
....
});
我是linq查詢的新手。 請讓我知道如何編寫此查詢。 謝謝
嘗試這個:
var records = (from t1 in T1
join t2 in T2
on t1.bid equals t2.bid
where t1.id == id
select new test { tid = t1.tid, bid = t1.bid });
foreach(var record in records)
{
record.t2 = (from t2 in T2
where t2.bid == record.bid
select new T2 { id = t2.id, bid = t2.bid }).ToList();
}
看來您基本上需要按bId
和tid
進行分組。
var records = (from c in T1
join y in T2 on c.bId equals y.bId
where y.id == id
select new {c.bId, c.tid, y})
.GroupBy(t => new { t.bId, t.tid })
.Select(g => new Result { bid = g.Key.bId, tid = g.Key.tid,
tests = g.Select(gg=>gg.y) }).ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.