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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM