IQueryable join 的问题

时间:2023-03-08 22:08:21
//定义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.*