1. public object GetListAdmin()
  2. {
  3. //return db_C56.Admins
  4. //   .Where(a => a.Status != "D").ToList();
  5. var query1 = db_C56.Admins.Join(db_C56.Area, a => a.AreaID, ar => ar.ID, (a, ar) => new
  6. {
  7. userName = a.UserName,
  8. pwd = a.Password,
  9. dName = a.DisplayName,
  10. areaId = a.AreaID,
  11. hasNode = a.HasNode,
  12. roleName = a.RoleName,
  13. status = a.Status,
  14. areaName = ar.Name
  15. });
  16. var query = from a in db_C56.Admins
  17. join ar in db_C56.Area
  18. on a.AreaID equals ar.ID
  19. where a.Status != "D"
  20. select new
  21. {
  22. userName = a.UserName,
  23. pwd = a.Password,
  24. dName = a.DisplayName,
  25. areaId = a.AreaID,
  26. hasNode = a.HasNode,
  27. roleName = a.RoleName,
  28. status = a.Status,
  29. areaName = ar.Name
  30. };
  31. return query.ToList().Select(C => new Admin
  32. {
  33. UserName = C.userName,
  34. Password = C.pwd,
  35. DisplayName = C.dName,
  36. AreaID = C.areaId,
  37. AreaPath = C.areaName,
  38. HasNode = C.hasNode,
  39. RoleName = C.roleName,
  40. Status = C.status,
  41. });
  42. }
  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 多层嵌套查询

  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. var query2 = dbContent.PostService.Where(p =>
  18. p.post_type == "product" &&
  19. (dbContent.OrderItemmetaService.Where(ot1 =>
  20. (dbContent.OrderItemsService.Where(ot2 =>
  21. ot2.order_item_type == "line_item" && (dbContent.PostService.Where(p1 =>
  22. p1.post_type == "shop_order" && p1.post_author == userid && p1.post_status == "wc-completed").Select(p1 => p1.ID).Contains(ot2.order_item_id))
  23. ).Select(ot2 => ot2.order_item_id).Contains(ot1.meta_id))
  24. ).Select(ot1 => ot1.meta_value).Contains(p.ID))
  25. ).Select(p => new
  26. {
  27. id = p.ID,
  28. name = p.post_title
  29. }).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)
         }
   )

LINQ,EF联合查询join的更多相关文章

  1. EF联合查询的新用法

    用EF很多年了,做联合查询时,只知道linq和lambda两种语法,今天朋友发了一个链接,打开看后发现是EF内置的新的关于联合查询的方法,赶紧抄录下来,以备后用. 现在先把这几种方法,各写一个例子,便 ...

  2. 【HIVE】(3)联合查询join、时间戳函数、字符串函数

    数据 t_join1.txt 1,a,1 2,b,2 3,c,4 t_join2.txt 1,2a 2,2b 3,2c 建表.导入: create table t_join1(id int, name ...

  3. EF 表联合查询 join

    有两张表m_Dept.m_User,联合查询 linq方式.EF方式 private void Add() { List<m_Dept> lst = new List<m_Dept& ...

  4. EF联合查询,如何设置条件过滤从表数据

    最近在使用EF进行联合查询过程中,遇到了一件不开心的事情. 已禁用懒加载 var post = await _repository.GetMyPostById(blogId, postId).AsNo ...

  5. EF 联合查询

    EF 文章表和标签表联合查询标签id在dis中的文章,还不知道性能如何 var query = tagRepo.Entities.Include("Tags").Where(t = ...

  6. 转:EntityFramework查询--联合查询(Join,GroupJoin)

    首先我们先看一下Join public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this ...

  7. EntityFramework查询--联合查询(Join,GroupJoin)

    首先我们先看一下Join public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this ...

  8. sqlalchemy多表联合查询(join)

    使用outerjoin instances = db.session.query(Instance.name, Instance.sep_status, User.email).outerjoin( ...

  9. linq中如何实现多个条件的联合查询

    目前接触处理数据这一块比较多,在处理内存中的数据源的时候我一般使用的是linq,linq使用起来像sql语句一样,用法简单,功能强大. 最近需要实现一个从两个不同的文件读取不同的数据,然后根据这两个数 ...

随机推荐

  1. SQL Server 2008 的gis函数

    居然不知道sql有gis函数,孤陋寡闻了 https://msdn.microsoft.com/zh-cn/library/bb933904.aspx   STContains(geometry 数据 ...

  2. 2016 系统设计第一期 (档案一)MVC bootstrap model弹出窗

    局部代码: <!-- 按钮触发模态框 --> <div style=""> <button class="btn btn-primary&q ...

  3. javascript面向对象思想2

    上篇说到面向对象可以帮我们梳理页面的逻辑的文章(http://www.cnblogs.com/hetaojs/p/6024013.html),很多朋友看了说我这种写法是初级的面向对象小儿科,确实是初级 ...

  4. c#3位一分(money)

     NumberFormatInfo num = new NumberFormatInfo();         num.NumberDecimalDigits = 2;         string ...

  5. Mvc设计模型与三层架构

    Mvc(Model-View-Controller):是软件架构的一中设计模式,对软件进行分割成3个层次:视图.模型.控制. 实现对软件的一种动态的设计,并且容易对软件进行扩展.后期的修改,使某些程序 ...

  6. 带括号的四则混合运算的算符优先算法-----java实现

    1:主方法 package com.baidu; import java.text.NumberFormat;import java.util.ArrayList;import java.util.S ...

  7. 1190: [HNOI2007]梦幻岛宝珠 - BZOJ

    Description 给你N颗宝石,每颗宝石都有重量和价值.要你从这些宝石中选取一些宝石,保证总重量不超过W,且总价值最大为,并输出最大的总价值. 数据范围:N<=100;W<=2^30 ...

  8. 一个 XSD 实例

    一个 XSD 实例 本节会为您演示如何编写一个 XML Schema.您还将学习到编写 schema 的不同方法. XML 文档 让我们看看这个名为 "shiporder.xml" ...

  9. springMVC从上传的Excel文件中读取数据

    示例:导入客户文件(Excle文件) 一.编辑customer.xlsx 二.在spring的xml文件设置上传文件大小 <!-- 上传文件拦截,设置最大上传文件大小 10M=10*1024*1 ...

  10. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...