Linq 多层嵌套查询

 var query1 = from p in dbContent.PostService
              where p.post_type == "product" &&
                  (from ot1 in dbContent.OrderItemmetaService
                   where
                       (from ot2 in dbContent.OrderItemsService
                        where ot2.order_item_type == "line_item" &&
                            (from p1 in dbContent.PostService
                             where p1.post_type == "shop_order" && p1.post_author == userid && p1.post_status == "wc-completed"
                             select p1.ID).Contains(ot2.order_id)
                        select ot2.order_item_id).Contains(ot1.meta_id)
                   select ot1.meta_value).Contains(p.ID)
              select new
              {
                  id = p.ID,
                  name = p.post_title
              };

 var query2 = dbContent.PostService.Where(p =>
     p.post_type == "product" &&
     (dbContent.OrderItemmetaService.Where(ot1 =>
         (dbContent.OrderItemsService.Where(ot2 =>
             ot2.order_item_type == "line_item" && (dbContent.PostService.Where(p1 =>
                 p1.post_type == "shop_order" && p1.post_author == userid && p1.post_status == "wc-completed").Select(p1 => p1.ID).Contains(ot2.order_item_id))
                 ).Select(ot2 => ot2.order_item_id).Contains(ot1.meta_id))
                 ).Select(ot1 => ot1.meta_value).Contains(p.ID))
                 ).Select(p => new
                 {
                     id = p.ID,
                     name = p.post_title

}).ToList();

Left Join 查询

from d in Doctors
join c in ((from t in Commentaries where t.State != 'D' group t by new { t.DoctorID } into g 
        select new {
                DoctorID = (Int64?)g.Key.DoctorID,
               Total = (Int32?)g.Sum(p => p.Rating),
                 Evaluate = (System.Double?)g.Average(p => p.Rating)
                           })) 
on new { UserID = d.UserID } equals new { UserID = (Int64)c.DoctorID }
into a_join from p in a_join.DefaultIfEmpty()
select new {
  d.ID,
  UserID = (Int64?)d.UserID,
  d.Name,
  Evaluate = ((int?)p.Evaluate ?? (int?)0)
}

Lambda表达式


Doctors .GroupJoin (Commentaries.Where (t => ((Int32)(t.State) != 68)).GroupBy ( t => new  
               {
                  DoctorID = t.DoctorID
               } ).Select ( g => new  
               {
                  DoctorID = (Int64?)(g.Key.DoctorID), 
                  Total = (Int32?)(g.Sum (p => p.Rating)), 
                  Evaluate = (Double?)(g.Average (p => p.Rating))
               } ),  d =>  new  {UserID = d.UserID},  c => new   { UserID = (Int64)(c.DoctorID)}, (d, a_join) => new   { d = d,  a_join = a_join})
               .SelectMany (temp0 => temp0.a_join.DefaultIfEmpty (), (temp0, p) => 
         new  
         {
            ID = temp0.d.ID, 
            UserID = (Int64?)(temp0.d.UserID), 
            Name = temp0.d.Name, 
            Evaluate = ((Int32?)(p.Evaluate) ?? (Int32?)0)
         }
   )




相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页