LINQ,EF联合查询join

  1. public object GetListAdmin()  
  2.         {  
  3.             //return db_C56.Admins  
  4.             //   .Where(a => a.Status != "D").ToList();  
  5.   
  6.             var query1 = db_C56.Admins.Join(db_C56.Area, a => a.AreaID, ar => ar.ID, (a, ar) => new  
  7.             {  
  8.                 userName = a.UserName,  
  9.                 pwd = a.Password,  
  10.                 dName = a.DisplayName,  
  11.                 areaId = a.AreaID,  
  12.                 hasNode = a.HasNode,  
  13.                 roleName = a.RoleName,  
  14.                 status = a.Status,  
  15.                 areaName = ar.Name  
  16.             });  
  17.   
  18.             var query = from a in db_C56.Admins  
  19.                         join ar in db_C56.Area  
  20.                         on a.AreaID equals ar.ID  
  21.                         where a.Status != "D"  
  22.                         select new  
  23.                         {  
  24.                             userName = a.UserName,  
  25.                             pwd = a.Password,  
  26.                             dName = a.DisplayName,  
  27.                             areaId = a.AreaID,  
  28.                             hasNode = a.HasNode,  
  29.                             roleName = a.RoleName,  
  30.                             status = a.Status,  
  31.                             areaName = ar.Name  
  32.                         };  
  33.             return query.ToList().Select(C => new Admin  
  34.             {  
  35.                 UserName = C.userName,  
  36.                 Password = C.pwd,  
  37.                 DisplayName = C.dName,  
  38.                 AreaID = C.areaId,  
  39.                 AreaPath = C.areaName,  
  40.                 HasNode = C.hasNode,  
  41.                 RoleName = C.roleName,  
  42.                 Status = C.status,  
  43.             });  
  44.         }  
[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. from v in Pdt_Versions  
  2. join t in Tb_TypeDics   
  3. on v.TypeName equals t.TypeName into ignored  
  4. from i in ignored.DefaultIfEmpty()  
  5. where v.Status != "D"  
  6. select new   
  7. {  
  8. ID = v.ID,  
  9. VersionName = v.VersionName,  
  10. VersionCode = v.VersionCode,  
  11. DownloadName = v.DownloadName,  
  12. DownloadURL = v.DownloadURL,  
  13. VType = v.VType,  
  14. TypeName = v.TypeName,  
  15. DisplyTypeName = i.DisplyTypeName,  
  16. }  

Linq 多层嵌套查询

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. var query1 = from p in dbContent.PostService  
  2.              where p.post_type == "product" &&  
  3.                  (from ot1 in dbContent.OrderItemmetaService  
  4.                   where  
  5.                       (from ot2 in dbContent.OrderItemsService  
  6.                        where ot2.order_item_type == "line_item" &&  
  7.                            (from p1 in dbContent.PostService  
  8.                             where p1.post_type == "shop_order" && p1.post_author == userid && p1.post_status == "wc-completed"  
  9.                             select p1.ID).Contains(ot2.order_id)  
  10.                        select ot2.order_item_id).Contains(ot1.meta_id)  
  11.                   select ot1.meta_value).Contains(p.ID)  
  12.              select new  
  13.              {  
  14.                  id = p.ID,  
  15.                  name = p.post_title  
  16.              };  
  17.   
  18. var query2 = dbContent.PostService.Where(p =>  
  19.     p.post_type == "product" &&  
  20.     (dbContent.OrderItemmetaService.Where(ot1 =>  
  21.         (dbContent.OrderItemsService.Where(ot2 =>  
  22.             ot2.order_item_type == "line_item" && (dbContent.PostService.Where(p1 =>  
  23.                 p1.post_type == "shop_order" && p1.post_author == userid && p1.post_status == "wc-completed").Select(p1 => p1.ID).Contains(ot2.order_item_id))  
  24.                 ).Select(ot2 => ot2.order_item_id).Contains(ot1.meta_id))  
  25.                 ).Select(ot1 => ot1.meta_value).Contains(p.ID))  
  26.                 ).Select(p => new  
  27.                 {  
  28.                     id = p.ID,  
  29.                     name = p.post_title  
  30.                 }).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)
         }
   )

======================================================================

多个left join

from d in Doctors
join f in Functions on new { FunctionID = d.FunctionID } equals new { FunctionID = f.ID } into b_join
from f in b_join.DefaultIfEmpty()
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 c in a_join.DefaultIfEmpty()
select new {
  d.ID,
  UserID = (Int64?)d.UserID,
  d.AvatarPic,
  d.Name,
  f.Title,
  f.ContentDescribe,
  Evaluate = ((int?)c.Evaluate ?? (int?)0)
}

Lambda表达式

Doctors
   .GroupJoin (
      Functions, 
      d => 
         new  
         {
            FunctionID = d.FunctionID
         }, 
      f => 
         new  
         {
            FunctionID = f.ID
         }, 
      (d, b_join) => 
         new  
         {
            d = d, 
            b_join = b_join
         }
   )
   .SelectMany (
      temp0 => temp0.b_join.DefaultIfEmpty (), 
      (temp0, f) => 
         new  
         {
            temp0 = temp0, 
            f = f
         }
   )
   .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))
               }
         ), 
      temp1 => 
         new  
         {
            UserID = temp1.temp0.d.UserID
         }, 
      c => 
         new  
         {
            UserID = (Int64)(c.DoctorID)
         }, 
      (temp1, a_join) => 
         new  
         {
            temp1 = temp1, 
            a_join = a_join
         }
   )
   .SelectMany (
      temp2 => temp2.a_join.DefaultIfEmpty (), 
      (temp2, c) => 
         new  
         {
            ID = temp2.temp1.temp0.d.ID, 
            UserID = (Int64?)(temp2.temp1.temp0.d.UserID), 
            AvatarPic = temp2.temp1.temp0.d.AvatarPic, 
            Name = temp2.temp1.temp0.d.Name, 
            Title = temp2.temp1.f.Title, 
            ContentDescribe = temp2.temp1.f.ContentDescribe, 
            Evaluate = ((Int32?)(c.Evaluate) ?? (Int32?)0)
         }
   )
原文地址:https://www.cnblogs.com/Alex80/p/5490014.html