//定义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 的问题的更多相关文章

  1. Entity Framework: Joining in memory data with DbSet

    转载自:https://ilmatte.wordpress.com/2013/01/06/entity-framework-joining-in-memory-data-with-dbset/ The ...

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

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

  3. EF INNER JOIN,LEFT JOIN,GROUP JOIN

    IQueryable<TOuter>的扩展方法中提供了 INNER JOIN,GROUP JOIN但是没有提供LEFT JOIN GROUP JOIN适用于一对多的场景,如果关联的GROU ...

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

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

  5. EntityFramework IEnumerable,IQueryable ,Include

    使用IQueryable using (var db = new CentaStaffEntities()) { IQueryable<Staff> queryablestaffs = d ...

  6. 构建属于自己的ORM框架之二--IQueryable的奥秘

    上篇文章标题乱起,被吐槽了,这次学乖了. 上篇文章中介绍了如何解析Expression生成对应的SQL语句,以及IQueryable的一些概念,以及我们所搭建的框架的思想等.但还没把它们结合并应用起来 ...

  7. Linq 的IQueryable和IEnumerable方式

    IEnumerable方式: public IEnumerable<WebManageUsers> GetWebManageUsers(ISpecification<WebManag ...

  8. 实战 EF(LINQ) 如何以子查询的形式来 Join

    如题,大多数网上关于 LINQ Join 的示例都是以 from x in TableA  join ... 这样的形式,这种有好处,也有劣势,就是在比如我们使用的框架如果已经封装了很多方法,比如分页 ...

  9. linq中如何在join中指定多个条件

    public ActionResult Edit(int id) { using (DataContext db = new DataContext(ConfigurationManager.Conn ...

随机推荐

  1. MFC 屏幕截图方法

    //获取当前屏幕的并且保存图片 LRESULT CFeetScanView::SaveViewBMP(WPARAM wParam, LPARAM lParam) { CRect rect; this- ...

  2. opensuse 13.1 安装配置从0开始

    主要目的为自己留作备份,仅作参考! 1. 输入法 托盘->输入法->配置 去掉除英语和Sunpinyin之外的输入法,配置Sunpinyin,使用双拼方案,重启fcitx. 另外需要禁用笔 ...

  3. Xml 序列化

    1 XML序列化只能序列化对象的公有属性,并且要求对象有一个无参的构造方法,否者无法反序列化. 2 [Serializable]和[NonSerialized]特性对XML序列化无效!所以使用XML序 ...

  4. 开源框架中常用的php函数

    类的自动加载后直接实例化 //自动加载类 function my_autoloader($class) { include $class . 'Class.php'; } spl_autoload_r ...

  5. HDU 5015

    http://acm.hdu.edu.cn/showproblem.php?pid=5015 矩阵是表示状态转移的利器 这题m很大,n非常小,所以开始的思考角度是能否从当前列推出下一列.有了这个角度, ...

  6. jquery省市联动,根据公司需求而写

    //author:guan //2015-05-25 //省市联动 //实用说明,页面引用如下js //<script src="../js/jquery-1.6.3.min.js&q ...

  7. haslayout

    什么是 haslayout ? haslayout 是Windows Internet Explorer渲染引擎的一个内部组成部分.在Internet Explorer中,一个元素要么自己对自身的内容 ...

  8. 《The Evolution of Lua》读书笔记 1

    lua的优点: 可移植性 容易嵌入 体积小 高效率 这些优点都来自于lua的设计目标:简洁.从Scheme获得了很多灵感,包括匿名函数,合理的语义域概念   lua前身: 巴西被禁运,引入计算机软件和 ...

  9. Max Points on a Line

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  10. gcc编译的四个阶段:预处理,编译,汇编,链接

    1:gcc编译的四个阶段:预处理,编译,汇编,链接 #vi file.c #gcc -E file.c -o file.i//-E查看且预处理后停止编译,-o生成目标文件,-i表示已预处理 #gcc  ...