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.*

  • 相关阅读:
    如何在mysql客户端即mysql提示符下执行操作系统命令
    干掉safedog命令
    面试题-----判断两个无环单链表是否交叉,如果交叉返回交叉点
    面试题-----单链表的反转
    面试题-----ICMP协议简介
    面试题-------SSL协议简介
    面试题-----求单链表的倒数第k个节点
    面试题---两个有序单链表的合并
    面试题---求一个串中的最大连续递增数字串
    面试题---两个大整数相乘
  • 原文地址:https://www.cnblogs.com/JasonLiao/p/IQueryable_join.html
Copyright © 2011-2022 走看看