日期:2014-05-17 浏览次数:20488 次
void Main() { var listA=new List<A>(){new A{ ID=1, Content="aaaaaaaaaa"}}; var listB=new List<B>() { new B{ ID=1, AID=1, UserID=2012}, new B{ ID=2, AID=1, UserID=2013}, new B{ ID=3, AID=1, UserID=2014}, }; var query=from a in listA join b in listB on a.ID equals b.AID into t select new { ID=a.ID, Content=a.Content, UserIDs=string.Join(",",t.Select(x=>x.UserID.ToString()).ToArray()) }; } public class A { public int ID{get;set;} public string Content{get;set;} } public class B { public int ID{get;set;} public int AID{get;set;} public int UserID{get;set;} }
------解决方案--------------------
当然,如果你喜欢用lamda表达式,也可以这么写:
var query=listA.GroupJoin(listB, a=>a.ID, b=>b.AID, (a,t)=>new { ID=a.ID, Content=a.Content, UserIDs=string.Join(",",t.Select(x=>x.UserID.ToString()).ToArray()) });