EF联合查询的新用法
用EF很多年了,做联合查询时,只知道linq和lambda两种语法,今天朋友发了一个链接,打开看后发现是EF内置的新的关于联合查询的方法,赶紧抄录下来,以备后用。
现在先把这几种方法,各写一个例子,便于大家参考:
linq:
var re = from o in db.Order
join d in db.OrderDetail
on o.Code equals d.OrderCode
join p in db.Product
on d.ProductID equals p.id
where o.Code == "20150326000096"
select new {
OrderCode = o.Code,
ProductName = p.Name,
SalePrice = p.SalePrice,
ProductQuantity = d.Quantity,
Amount = d.Amount
};
SELECT
[Extent1].[ProductID] AS [ProductID],
[Extent1].[OrderCode] AS [OrderCode],
[Extent2].[Name] AS [Name],
[Extent2].[SalePrice] AS [SalePrice],
[Extent1].[Quantity] AS [Quantity],
[Extent1].[Amount] AS [Amount]
FROM [dbo].[OrderDetails] AS [Extent1]
INNER JOIN [dbo].[Products] AS [Extent2] ON [Extent1].[ProductID] = [Extent2].[id]
WHERE ([Extent1].[OrderCode] IS NOT NULL) AND (N'20150326000096' = [Extent1].[OrderCode])
lambda:
re = db.OrderDetail.Where(d => d.OrderCode == "20150326000096").Select(d => new {
OrderCode = d.Order.Code,
ProductName = d.Product.Name,
SalePrice = d.Product.SalePrice,
ProductQuantity = d.Quantity,
Amount = d.Amount
});
SELECT
[Extent1].[ProductID] AS [ProductID],
[Extent1].[OrderCode] AS [OrderCode],
[Extent2].[Name] AS [Name],
[Extent2].[SalePrice] AS [SalePrice],
[Extent1].[Quantity] AS [Quantity],
[Extent1].[Amount] AS [Amount]
FROM [dbo].[OrderDetails] AS [Extent1]
INNER JOIN [dbo].[Products] AS [Extent2] ON [Extent1].[ProductID] = [Extent2].[id]
WHERE N'20150326000096' = [Extent1].[OrderCode]
内置join方法:
var re = db.Employees.Join(db.Orders, e => e.EmployeeID, o => o.EmployeeID, (e, o) => new { EmployeeID = e.EmployeeID, OrderID = o.OrderID, Address = o.ShipAddress, EmployeeAddress = e.Address });
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]
内置Join用法2:
var re = db.Employees.Join(db.Orders, e => e.EmployeeID, o => o.EmployeeID, (e, o) => new { EmployeeID = e.EmployeeID, OrderID = o.OrderID });
SELECT
[Extent1].[EmployeeID] AS [EmployeeID],
[Extent1].[OrderID] AS [OrderID]
FROM [dbo].[Orders] AS [Extent1]
WHERE [Extent1].[EmployeeID] IS NOT NULL
内置的GroupJoin方法:
var re = db.Categories.GroupJoin(db.Products, c => c.CategoryID, p => p.CategoryID, (c, p) => new { CategoryID = c.CategoryID, ProductList = p });
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 ASint) 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
Join和GroupJoin方法还没用过,代码是照搬的,以后应用中发现了什么再来修改吧,先贴出来备用
参考文章:EntityFramework查询--联合查询(Join,GroupJoin)
EF联合查询的新用法的更多相关文章
- EF联合查询,如何设置条件过滤从表数据
最近在使用EF进行联合查询过程中,遇到了一件不开心的事情. 已禁用懒加载 var post = await _repository.GetMyPostById(blogId, postId).AsNo ...
- EF 联合查询
EF 文章表和标签表联合查询标签id在dis中的文章,还不知道性能如何 var query = tagRepo.Entities.Include("Tags").Where(t = ...
- LINQ,EF联合查询join
public object GetListAdmin() { //return db_C56.Admins // .Where(a => a.Status != "D") ...
- EF 表联合查询 join
有两张表m_Dept.m_User,联合查询 linq方式.EF方式 private void Add() { List<m_Dept> lst = new List<m_Dept& ...
- Linq和EF 做 单一条件查询 和 复合条件 查询 以及 多表 联合查询 示例
单一条件查询: var table2Object = (from t1 in db.table1 join t2 in db.table2 on t1.id equals t2.id select t ...
- 创建ASP.NET Core MVC应用程序(5)-添加查询功能 & 新字段
创建ASP.NET Core MVC应用程序(5)-添加查询功能 & 新字段 添加查询功能 本文将实现通过Name查询用户信息. 首先更新GetAll方法以启用查询: public async ...
- 【转】Mysql联合查询union和union all的使用介绍
Mysql的联合查询命令UNION和UNION ALL,总结了使用语法和注意事项,以及学习例子和项目例子,需要的朋友可以参考下 一.UNION和UNION ALL的作用和语法 UNION 用于合... ...
- SQL联合查询(内联、左联、右联、全联)的语法(转)
最近在做一个比较复杂的业务,涉及的表较多,于是在网上找了一些sql联合查询的例子进行研究使用. 概述: 联合查询效率较高,举例子来说明联合查询:内联inner join .左联left outer j ...
- SQL联合查询(内联、左联、右联、全联)的语法
联合查询效率较高,举例子来说明联合查询:内联inner join .左联left outer join .右联right outer join .全联full outer join 的好处及用法. 联 ...
随机推荐
- LeetCode【217. Contains Duplicate】
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- Python chr() ord() unichr()
chr()函数用一个范围在range(256)内的(就是0-255)整数作参数,返回一个对应的字符. unichr()跟它一样,只不过返回的是Unicode字符,这个从Python 2.0才加入的un ...
- Jquery tab 选项卡 无刷新切换
转载的 演示地址:http://www.freejs.net/demo/29/index.html 首页>>TAB标签>>jquery实现简单的Tab切换菜单(2013-09- ...
- pt-query-digest用法
pt-query-digest用法 http://blog.csdn.net/wxc20062006/article/details/52387505
- [Spring MVC] - Annotation验证
使用Spring MVC的Annotation验证可以直接对view model的简单数据验证,注意,这里是简单的,如果model的数据验证需要有一些比较复杂的业务逻辑性在里头,只是使用annotat ...
- Ubuntu 16.10 在 VMware 上无法安装的解决办法
参考:http://askubuntu.com/questions/840822/ubuntu-16-10-doesnt-work-in-virtual-machine-vmware 1- Edit ...
- ruby中http请求方法整理
#POST请求 请求包是json包 返回body并转换成json对象def post_json *args uri = URI.parse args[0] req = Net::HTTP::Post. ...
- zero ice 如何获取本地以及对端IP 地址及端口号
// 在客户端显示端点连接IP及端口号信息 static void show_endpoint(const Ice::ObjectPrx &proxy) { Ice::ConnectionPt ...
- case语句
case语句是多分支选择语句,if语句只有两个分支可供选择,而实际问题中常常需要用到多分支选择结构.例如,学生成绩分类(90分以上为A,--):人口统计分类(按年龄分为老.中.青.少.幼)等.当然这些 ...
- (String) 205.Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...