日期:2014-05-20  浏览次数:21202 次

linq to sql多表级联查询
有个表A,表B,表C。A里面是sid,pid,username.B里面是sname,c里面是pname。要求级联查询出sname,pname新手刚接触linq求指导。

------解决方案--------------------
C# code

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
          }

------解决方案--------------------
探讨
引用:

C# code

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
}


那个大写的A,B,C是一个临时变量还是??

------解决方案--------------------
C# code
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
          }

------解决方案--------------------
C# code

    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; }
    }

------解决方案--------------------
探讨
C# code

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
}

------解决方案--------------------
那就对B连两次
C# code

            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}
                );