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

LINQ 内连接+右连接
朋友问我一个问题,三张表 A B C  A和B内连接,然后和C右链接 A的主键Id  B和C中的AId 是外键,我这么写对不对?? 还有没有更好的写法??


using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<A> aas = new List<A>()
                              {
                                  new A(){ Id=1, AName="AName1"},
                                  new A(){ Id=2, AName="AName2"},
                                  new A(){ Id=3, AName="AName3"},
                                  new A(){ Id=4, AName="AName4"},
                              };

            List<B> bbs = new List<B>()
                              {
                                  new B(){ AId=1, BName="BName1"},
                                  new B(){ AId=2, BName="BName2"},
                                  new B(){ AId=3, BName="BName3"},
                                  new B(){ AId=4, BName="BName4"},
                              };

            List<C> ccs = new List<C>()
                              {
                                  new C(){ AId=1, CName="CName1"},
                                  new C(){ AId=2, CName="CName2"},
                                  new C(){ AId=3, CName="CName3"},
                                  new C(){ AId=4, CName="CName4"},
                                  new C(){ AId=5, CName="CName5"}
                              };

            var query = from a in aas
                        join b in bbs on a.Id equals b.AId into d
                        from e in d.DefaultIfEmpty()