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




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值