zoukankan      html  css  js  c++  java
  • 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 = 0, Quantity = 0, 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 = 0, Quantity = 0, 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 = 0, Quantity = 0, 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(2).Take(2).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.*

  • 相关阅读:
    InSAR 大气校正数据 GACOS
    java通用的方法整理
    Jquery实现select左右栏的添加移除
    kendo ui之grid列表
    kendo-ui学习笔记(一)
    kendo-ui学习笔记——题记
    chrome driver驱动地址
    适合引入缓存的业务及影响
    个人测试方法论202108
    Redis缓存
  • 原文地址:https://www.cnblogs.com/JasonLiao/p/IQueryable_join.html
Copyright © 2011-2022 走看看