日期:2014-05-20 浏览次数:21202 次
var linq=from a in A from b in B from c in C where a.sid=b.id and a.pid=c.id select new{ b.sname, c.pname }
------解决方案--------------------
var linq=from a in A join b in B on a.sid=b.id join c in C on a.pid=c.id select new{ b.sname, c.pname }
------解决方案--------------------
public class _2011_12_12_01 : TestUnit { public override void Run() { var a = new List<A>(); var b = new List<B>(); var c = new List<C>(); a.Add(new A { sid = 1, pid = 1, username = "u1" }); a.Add(new A { sid = 2, pid = 2, username = "u2" }); a.Add(new A { sid = 3, pid = 3, username = "u3" }); b.Add(new B { sid = 1, sname = "s1" }); b.Add(new B { sid = 2, sname = "s2" }); b.Add(new B { sid = 3, sname = "s3" }); c.Add(new C { pid = 1, pname = "p1" }); c.Add(new C { pid = 2, pname = "p2" }); c.Add(new C { pid = 3, pname = "p3" }); var results = a.Join( b, xa => xa.sid, xb => xb.sid, (xa, xb) => new { username = xa.username, sname = xb.sname, pid = xa.pid } ).Join( c, xa => xa.pid, xc => xc.pid, (xa, xc) => new { username = xa.username, sname = xa.sname, pname = xc.pname }); foreach (var item in results) { Console.WriteLine("{0,10}{1,10}{2,10}", item.username, item.sname, item.pname); } } } class A { public int sid { get; set; } public int pid { get; set; } public string username { get; set; } } class B { public int sid { get; set; } public string sname { get; set; } } class C { public int pid { get; set; } public string pname { get; set; } }
------解决方案--------------------
results = a.Join( b, xa => xa.sid, xb => xb.vid, (xa, xb) => new { username = xa.username, sname = xb.name, pid = xa.pid } ).Join( b, xa => xa.pid, xb => xb.vid, (xa, xb) => new { username = xa.username, sname = xa.sname, pname = xb.name} );