LEFT OUTER JOIN in LINQ

后端 未结 22 2455
臣服心动
臣服心动 2020-11-21 04:49

How to perform left outer join in C# LINQ to objects without using join-on-equals-into clauses? Is there any way to do that with where clause? Corr

22条回答
  •  既然无缘
    2020-11-21 05:16

    Overview: In this code snippet, I demonstrate how to group by ID where Table1 and Table2 have a one to many relationship. I group on Id, Field1, and Field2. The subquery is helpful, if a third Table lookup is required and it would have required a left join relationship. I show a left join grouping and a subquery linq. The results are equivalent.

    class MyView
    {
    public integer Id {get,set};
        public String Field1  {get;set;}
    public String Field2 {get;set;}
        public String SubQueryName {get;set;}                           
    }
    
    IList list = await (from ci in _dbContext.Table1
                                                   join cii in _dbContext.Table2
                                                       on ci.Id equals cii.Id
    
                                                   where ci.Field1 == criterion
                                                   group new
                                                   {
                                                       ci.Id
                                                   } by new { ci.Id, cii.Field1, ci.Field2}
    
                                               into pg
                                                   select new MyView
                                                   {
                                                       Id = pg.Key.Id,
                                                       Field1 = pg.Key.Field1,
                                                       Field2 = pg.Key.Field2,
                                                       SubQueryName=
                                                       (from chv in _dbContext.Table3 where chv.Id==pg.Key.Id select chv.Field1).FirstOrDefault()
                                                   }).ToListAsync();
    
    
     Compared to using a Left Join and Group new
    
    IList list = await (from ci in _dbContext.Table1
                                                   join cii in _dbContext.Table2
                                                       on ci.Id equals cii.Id
    
                           join chv in _dbContext.Table3
                                                      on cii.Id equals chv.Id into lf_chv
                                                    from chv in lf_chv.DefaultIfEmpty()
    
                                                   where ci.Field1 == criterion
                                                   group new
                                                   {
                                                       ci.Id
                                                   } by new { ci.Id, cii.Field1, ci.Field2, chv.FieldValue}
    
                                               into pg
                                                   select new MyView
                                                   {
                                                       Id = pg.Key.Id,
                                                       Field1 = pg.Key.Field1,
                                                       Field2 = pg.Key.Field2,
                                                       SubQueryName=pg.Key.FieldValue
                                                   }).ToListAsync();
    

提交回复
热议问题