IQueryable join 的问题
//定义OrderDetailsTable model类
public class OrderDetailsTable
{
public int OrderID { get; set; } public DateTime? OrderDate { get; set; } public string ShipCountry { get; set; } public string ProductName { get; set; } public List<string> ProductNames { get; set; } public decimal UnitPrice { get; set; } public int Quantity { get; set; } public string CategoryName { get; set; }
}
using (NORTHWNDEntities db = new NORTHWNDEntities())
{
IQueryable<OrderDetailsTable> OrderDetailsQuery = from o in db.Orders
join od in db.OrderDetails on o.OrderID equals od.OrderID
join p in db.Products on od.ProductID equals p.ProductID
join c in db.Categories on p.CategoryID equals c.CategoryID
select (
new OrderDetailsTable
{
OrderID = o.OrderID,
OrderDate = o.OrderDate,
ShipCountry = o.ShipCountry,
UnitPrice = od.UnitPrice.Value,
Quantity = od.Quantity.Value,
ProductName = p.ProductName,
CategoryName = c.CategoryName,
//ProductNames = db.Products.Where(pn => pn.ProductID == od.ProductID).Select(pn=>pn.ProductName).ToList(),
});
List<string> countryNames = new List<string> { "China", "Spain" }; var chinaOrder = OrderDetailsQuery.Where(o => o.ShipCountry.Equals("China")).Where(o => countryNames.Any(x => o.ShipCountry.Contains(x)));
var spainOrder = OrderDetailsQuery.Where(o => o.ShipCountry.Equals("Spain")).Where(o => countryNames.Any(x => o.ShipCountry.Contains(x))); var ChaiUpdateOrder = from od in db.OrderDetails
join p in db.Products on od.ProductID equals p.ProductID
where p.ProductName == "ChaiUpdate"
select
(
new OrderDetailsTable
{
OrderID = od.OrderID,
OrderDate = null,
ShipCountry = "",
UnitPrice = ,
Quantity = ,
ProductName = p.ProductName,
CategoryName = "",
}
); var BostonCrabMeatOrder = from od in db.OrderDetails
join p in db.Products on od.ProductID equals p.ProductID
where p.ProductName == "Boston Crab Meat"
select
(
new OrderDetailsTable
{
OrderID = od.OrderID,
OrderDate = null,
ShipCountry = "",
UnitPrice = ,
Quantity = ,
ProductName = p.ProductName,
CategoryName = "",
}
); var TeatimeChocolateBiscuitsUpdateOrder = from od in db.OrderDetails
join p in db.Products on od.ProductID equals p.ProductID
where p.ProductName == "Teatime Chocolate BiscuitsUpdate"
select
(
new OrderDetailsTable
{
OrderID = od.OrderID,
OrderDate = null,
ShipCountry = "",
UnitPrice = ,
Quantity = ,
ProductName = p.ProductName,
CategoryName = "",
}
); IQueryable<OrderDetailsTable> productFilterOrder = ChaiUpdateOrder.Union(BostonCrabMeatOrder).Union(TeatimeChocolateBiscuitsUpdateOrder); var productFilterOrderFirst = productFilterOrder.GroupBy(o => o.OrderID).Select(o => o.First()); var spainProductOrder = from od in spainOrder
join pfo in productFilterOrder on od.OrderID equals pfo.OrderID
select od; var orderAll = chinaOrder.Union(spainProductOrder).OrderBy(o => o.OrderID); int count = orderAll.Count();
var orderList = orderAll.ToList(); var pagedList = orderAll.Skip().Take().ToList();
使用IQueryable时遇到两个问题
1. The 'Distinct' operation cannot be applied to the collection ResultType of the specified argument.
当查询结果包含引用类型时,不能直接使用union,Distinct只能处理简单类型。
所以在OrderDetailsQuery 中,不能有List。
2. The type 'EFSample.OrderDetailsTable' appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order.
当查询结果集排序或者字段不一致时,无法union。因此在ChaiUpdateOrder 中虽然不需要其他信息,还是要按照union的格式写全。
3. 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);
var IQueryJoin = spainOrder.Join(productFilterOrder, a => a.OrderID, b => b.OrderID, (a, b) => a);
A Join B,A.Key Compare with B.Key, Select A.*
IQueryable join 的问题的更多相关文章
- Entity Framework: Joining in memory data with DbSet
转载自:https://ilmatte.wordpress.com/2013/01/06/entity-framework-joining-in-memory-data-with-dbset/ The ...
- EntityFramework查询--联合查询(Join,GroupJoin)
首先我们先看一下Join public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this ...
- EF INNER JOIN,LEFT JOIN,GROUP JOIN
IQueryable<TOuter>的扩展方法中提供了 INNER JOIN,GROUP JOIN但是没有提供LEFT JOIN GROUP JOIN适用于一对多的场景,如果关联的GROU ...
- 转:EntityFramework查询--联合查询(Join,GroupJoin)
首先我们先看一下Join public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this ...
- EntityFramework IEnumerable,IQueryable ,Include
使用IQueryable using (var db = new CentaStaffEntities()) { IQueryable<Staff> queryablestaffs = d ...
- 构建属于自己的ORM框架之二--IQueryable的奥秘
上篇文章标题乱起,被吐槽了,这次学乖了. 上篇文章中介绍了如何解析Expression生成对应的SQL语句,以及IQueryable的一些概念,以及我们所搭建的框架的思想等.但还没把它们结合并应用起来 ...
- Linq 的IQueryable和IEnumerable方式
IEnumerable方式: public IEnumerable<WebManageUsers> GetWebManageUsers(ISpecification<WebManag ...
- 实战 EF(LINQ) 如何以子查询的形式来 Join
如题,大多数网上关于 LINQ Join 的示例都是以 from x in TableA join ... 这样的形式,这种有好处,也有劣势,就是在比如我们使用的框架如果已经封装了很多方法,比如分页 ...
- linq中如何在join中指定多个条件
public ActionResult Edit(int id) { using (DataContext db = new DataContext(ConfigurationManager.Conn ...
随机推荐
- maven 配置
四.eclipse配置maven eclipse---window---maven------User Settings: 之前设置的仓库的位置: 五.idea15配置maven idea14---s ...
- business knowledge
Finance knowledge Trading---At the core of our business model is Trading, which involves the buying ...
- codeforces 723E (欧拉回路)
Problem One-Way Reform 题目大意 给一张n个点,m条边的无向图,要求给每条边定一个方向,使得最多的点入度等于出度,要求输出方案. 解题分析 最多点的数量就是入度为偶数的点. 将入 ...
- [GodLove]Wine93 Tarining Round #4
比赛链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=44903#overview 题目来源: 2011 Asia ChengDu R ...
- Magento显示多货币,Magento 多货币设置
System - Configuration - Currency Setup 在右边Currency Options里的Allowed currencies勾选, 然后 System - Manag ...
- python数据结构
. 数据结构¶ .1. 深入列表¶ 链表类型有很多方法,这里是链表类型的所有方法: list.append(x) 把一个元素添加到链表的结尾,相当于 a[len(a):] = [x] . list ...
- 安装Fedora 24后必要的设置
安装Fedora 24后必要的设置 导读 Fedora 是一个 Linux 发行版,是一款由全球社区爱好者构建的面向日常应用的快速.稳定.强大的操作系统.它允许任何人自由地使用.修改和重发布,无论现在 ...
- C#中as用法
在程序中,进行类型转换时常见的事,C#支持基本的强制类型转换方法,例如 Object obj1 = new NewType();NewType newValue = (NewType)obj1;这样强 ...
- Query 一些简单的效果
Query 一些简单的效果 $(selector).hide(speed,callback); 隐藏 $(selector).show(speed,callback); 显示 $(selector). ...
- 最小生成树——prim算法
prim算法是选取任意一个顶点作为树的一个节点,然后贪心的选取离这棵树最近的点,直到连上所有的点并且不够成环,它的时间复杂度为o(v^2) #include<iostream>#inclu ...