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

  • 相关阅读:
    练习 字符串存入字典 数组的降序 倒序 字符串目录存不存在 数组中文排序
    随机 随机获得100个50-100的数字字符串,存到数组并输出
    练习 字符串10题
    把一个字符串反输出abc123.xyz789输出987zyx.321cba
    把NSString *string=@"2013 年 05 月 05 日";以2013-05-05输出
    dephi cef3获取Post数据
    Delphi下 多显示器,将窗体显示于第二个显示器
    Delphi下POS机
    SQLConnection 连接 SQLite
    cxGrid主视图取得从视图
  • 原文地址:https://www.cnblogs.com/JasonLiao/p/IQueryable_join.html
Copyright © 2011-2022 走看看