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

首先我们先看一下Join

public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector);

第一个参数outer就是你连接的左面的集合,第二个inner是你要与之连接的集合,第三个outerKeySelector就是要用outer的哪个键来进行连接,第四个innerKeySelector同理,最后一个就是返回的类型。用法如下:

NorthwindEntities dbContext = new NorthwindEntities();
var data = dbContext.Employees.Join(dbContext.Orders, e => e.EmployeeID, o => o.EmployeeID, (e, o) => new { EmployeeID = e.EmployeeID, OrderID = o.OrderID, Address = o.ShipAddress, EmployeeAddress = e.Address });

上面的代码对应的SQL语句如下:

SELECT
    [Extent1].[EmployeeID] AS [EmployeeID],
    [Extent2].[OrderID] AS [OrderID],
    [Extent2].[ShipAddress] AS [ShipAddress],
    [Extent1].[Address] AS [Address]
FROM  [dbo].[Employees] AS [Extent1]
INNER JOIN [dbo].[Orders] AS [Extent2] ON [Extent1].[EmployeeID] = [Extent2].[EmployeeID]

很明显是用的内连接方式,但是会不会有其他方式呢?那么再来一段代码看看:

var data = dbContext.Employees.Join(dbContext.Orders, e => e.EmployeeID, o => o.EmployeeID, (e, o) => new { EmployeeID = e.EmployeeID, OrderID = o.OrderID });

对应的SQL语句如下:

SELECT
    [Extent1].[EmployeeID] AS [EmployeeID],
    [Extent1].[OrderID] AS [OrderID]
FROM [dbo].[Orders] AS [Extent1]
WHERE [Extent1].[EmployeeID] IS NOT NULL

看到了吧,没有inner join了,本意是要查找Employee对应的订单,但是上面的代码只返回了EmpoloyeeID和OrderID,因为查找Employee对应的订单,本来就可以直接在Orders中查找的,所以EF还是对此代码做了优化,免去了inner join。

既然连接已经写好了,那么要对返回的结果进行筛选你可以调用Where等扩展方法了。

接下来看看GroupJoin

public static IQueryable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IQueryable<TOuter> outer, IEnumerable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, IEnumerable<TInner>, TResult>> resultSelector);

来看个例子:Products表有一个CategoryID的属性,但是Category中并没要存储对应的ProductID,如果要查找Category下的Product我们该怎么做呢?如下:

var data = dbContext.Categories.GroupJoin(dbContext.Products, c => c.CategoryID, p => p.CategoryID, (c, p) => new { CategoryID = c.CategoryID, ProductList = p });

对应的SQL语句如下:

SELECT
[Project1].[CategoryID] AS [CategoryID],
[Project1].[C1] AS [C1],
[Project1].[ProductID] AS [ProductID],
[Project1].[ProductName] AS [ProductName],
[Project1].[SupplierID] AS [SupplierID],
[Project1].[CategoryID1] AS [CategoryID1],
[Project1].[QuantityPerUnit] AS [QuantityPerUnit],
[Project1].[UnitPrice] AS [UnitPrice],
[Project1].[UnitsInStock] AS [UnitsInStock],
[Project1].[UnitsOnOrder] AS [UnitsOnOrder],
[Project1].[ReorderLevel] AS [ReorderLevel],
[Project1].[Discontinued] AS [Discontinued]
FROM ( SELECT
    [Extent1].[CategoryID] AS [CategoryID],
    [Extent2].[ProductID] AS [ProductID],
    [Extent2].[ProductName] AS [ProductName],
    [Extent2].[SupplierID] AS [SupplierID],
    [Extent2].[CategoryID] AS [CategoryID1],
    [Extent2].[QuantityPerUnit] AS [QuantityPerUnit],
    [Extent2].[UnitPrice] AS [UnitPrice],
    [Extent2].[UnitsInStock] AS [UnitsInStock],
    [Extent2].[UnitsOnOrder] AS [UnitsOnOrder],
    [Extent2].[ReorderLevel] AS [ReorderLevel],
    [Extent2].[Discontinued] AS [Discontinued],
    CASE WHEN ([Extent2].[ProductID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
    FROM  [dbo].[Categories] AS [Extent1]
    LEFT OUTER JOIN [dbo].[Products] AS [Extent2] ON [Extent1].[CategoryID] = [Extent2].[CategoryID]
)  AS [Project1]
ORDER BY [Project1].[CategoryID] ASC, [Project1].[C1] ASC

不明白外面为什么又嵌套一层select,完全可以将排序放到内侧的查询中的嘛,然后把外面查询给去掉就得了!

时间: 2024-10-11 12:52:52

转:EntityFramework查询--联合查询(Join,GroupJoin)的相关文章

MySQL的查询,子查询,联结查询,联合查询

一.mysql查询的五种子句where(条件查询).having(筛选).group by(分组).order by(排序).limit(限制结果数) 二.子查询1.where 子查询SELECT * FROM tb1 WHERE cat_id IN (SELECT max(id) FROM tb2 GROUP BY cat_id); 2.from 子查询SELECT t2_id FROM (SELECT t2_id FROM tb2 ORDER BY t2_id DESC); 3.exists

MySql学习 - 查询/子查询/连接查询/联合查询

数据库查询 设定两张数据库表 第一个表格user包含: user_id username age sex 1 Alps1992 22 man 第二个表格toy包含 user_id toyname 1 OnePiece 普通查询: 查询关键字: AS, SUM, DESC, GROUP BY, ORDER BY, AVG, MIN, MAX, COUNT, LIMIT; 关键字查询例子 select * from user as u order by age limit 5; // as 用来做别

MySQL数据库8 -子查询,联合查询

一 使用IN关键字的子查询 问题: 查询游戏类型是'棋牌类' 的游戏的分数信息 - 游戏分数表中并未包含游戏类型信息 思路一:采用链接查询 思路二: 分两步进行,首先找到所以'棋牌类'游戏的编号,再以这一组编号为查询依据完成查询 select * from scores where gno in (select gno from games where gtype ='棋牌') 例:查询没有参与5号游戏的玩家QQ select user_qq from users where user_qq n

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

首先我们先看一下Join public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Fu

mysql连接查询,子查询,联合查询

一.连接查询(多表查询) emp表 dept表 1.交叉连接 实际上,交叉连接是将两个表不设定任何条件的连接结果. 交叉连接通常也被叫做"笛卡尔积"--数学上可能比较多. 语法: from  表1  [cross]  join  表2  ; //可见交叉连接只是没有on条件而已. cross这个词也可以省略,还可以使用inner这个词代替 2.内连接: 语法: from  表1  [inner]  join  表2  on  表1.字段1=表2.字段2: 含义:找出(过滤)在交叉连接的

EF 表联合查询 join

有两张表m_Dept.m_User,联合查询 linq方式.EF方式 private void Add() { List<m_Dept> lst = new List<m_Dept>{ new m_Dept { DName = "开发", ID = "1" }, new m_Dept { DName = "测试", ID = "2" }, new m_Dept { DName = "财务&qu

数据库之联合查询和连接查询

http://blog.csdn.net/memgxingfeixiang/article/details/52765208 1.JOIN和UNION区别join 是两张表做交连后里面条件相同的部分记录产生一个记录集,union是产生的两个记录集(字段要一样的)并在一起,成为一个新的记录集. JOIN用于按照ON条件联接两个表,主要有四种:INNER JOIN:内部联接两个表中的记录,仅当至少有一个同属于两表的行符合联接条件时,内联接才返回行.我理解的是只要记录不符合ON条件,就不会显示在结果集

2015-10-19 SQL(新建数据库、创建表、注释、查询语句、新增、更新、删除、联合查询)

1.认识数据库并新建: 1)打开数据库,连接到服务器. 2)服务里类型不用管. 3)服务器名称:打个点“.”表示服务器在本地计算机,如果是托管在别人的服务器上,就输入服务器IP地址. 4)身份验证:如果是windows身份验证,就需要有管理员权限.一般托管在别人的服务器上,(包括正常情况下),都是用SQL身份验证,需要输入sa和密码(密码就是安装时让输入的密码). 2.  建立数据库: 在建立数据库名称时(各种名称时),都遵守一个命名规则: 1)用英文命名 2)每个单词的首字母大写(驼峰原则:首

SQL联合查询中的关键语法(转)

联合查询效率较高.以下例子来说明联合查询的好处 t1表结构(用户名,密码)    userid int         username   varchar(20)     password       varchar(20) 1                    jack                           jackpwd 2                    owen                        owenpwd t3表结构(用户积分,等级)    u