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 ...
随机推荐
- 通过CSS实现小动物
此例演示的是通过CSS实现动物头像,效果如下: 好了,上代码: html代码: <html> <head> <meta charset="utf-8" ...
- DotNetBar中collapsibleSplitContainer的问题
如果有两个collapsibleSplitContainer,并且将splitwidth都设置为1,则只有第一个起作用,如,窗体设计中的图是: 而执行的图是: 解决办法:在窗体load里手动将第二个c ...
- struts2 配置拦截器
第一步:继承MethodFilterInterceptor写自己的自定义拦截器 import org.apache.struts2.ServletActionContext; import com.o ...
- Highcharts使用指南
统计分析报表Highcharts使用指南 一.前言(Preface)阅览本文,您可以了解:1.Highcharts使用方法2.Highcharts数据动态加载3.Highcharts自动刷新数据4.H ...
- vs2010设置断点进行调试时不起作用
1.打开vs2010 2.点击web下的“属性” 3.点击“生成” 4.点击最下方的“高级” 5.在“输出”-调试信息中选择“full”,点击确定按钮即可
- iOS学习之iOS沙盒(sandbox)机制和文件操作(二)
1.获取程序的Home目录 NSString *homeDirectory = NSHomeDirectory(); NSLog(@"path:%@", homeDirectory ...
- Gradle简介和安装
一.Gradle介绍 Gradle是一个基于JVM的构建工具,它提供了:像Ant一样,通用灵活的构建工具,可以切换的,基于约定的构建框架,强大的多工程构建支持,基于Apache Ivy的强大的依赖管理 ...
- response 设置头的类型 (转)
Response.ContentType 详细列表 不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 text/html 也就是网页格式.代码如: <% r ...
- libevent之丢失header问题
本文为原创,转载请注明:http://www.cnblogs.com/gistao/ 背景 分享一个hhvm使用http server方式来处理请求的问题及对应的patch.hhvm3+版本支持fas ...
- iOS 自定义选项卡-CYLTabBarController
正常的选项卡流程 cocoapods就不说了 创建一个CYLTabBarControllerConfig类 #import <Foundation/Foundation.h> #impor ...