日期:2014-05-17 浏览次数:20918 次
void Main()
{
var list1=new List<tb1>
{
new tb1{ id=1, name="a"},
new tb1{ id=2, name="b"},
new tb1{ id=3, name="c"},
new tb1{ id=4, name="d"},
};
var list2=new List<tb2>
{
new tb2{ id=1,pid=1, name="aa"},
new tb2{ id=2,pid=1, name="ab"},
new tb2{ id=3,pid=2,name="ba"},
new tb2{ id=4,pid=2,name="bb"},
new tb2{ id=5,pid=5,name="ea"},
new tb2{ id=6,pid=6,name="fa"},
};
var query=(from t1 in list1
join t2 in list2
on t1.id equals t2.pid into t
select new
{
id=t==null?"-":t1.id.ToString(),
pname=t==null?"":t1.name,
name=t==null?"":string.Join(",",t.Select(x=>x.name).ToArray())
}).Concat(Enumerable.Range(0,1).Select(s=>
new
{
id="-",
pname="-",
name=string.Join(",",list2.Where(l=>!list1.Select(x=>x.id).Contains(l.pid)).Select(x=>x.name).ToArray())
}));
}
class tb1
{
public int id {get;set;}