我正在编写一个 ASP.NET 网页应用程序,在其中,我有一个大量的 LINQ to Entities 查询。此查询从数据库中的表中提取数据,对其进行过滤,对数据进行两次分组,并向结果集添加额外的属性。然后我遍历表,输出行。
查询很大,抱歉:
accountOrders = db.EventOrders
.Where(order => order.EventID == eventID)
.OrderBy(order => order.ProductCode)
.GroupBy(order => new { order.AccountNum, order.Exhibitor, order.Booth })
.Select(orders =>
new {
Key = orders.Key,
ProductOrders = orders
.GroupBy(order => new { order.ProductCode, order.Product, order.Price })
.Select(productOrders =>
new {
Key = productOrders.Key,
Quantity = productOrders.Sum(item => item.Quantity),
HtmlID = String.Join(",", productOrders.Select(o => (o.OrderNum + "-" + o.OrderLine))),
AssignedLines = productOrders.SelectMany(order => order.LineAssignments)
})
})
.Select(account =>
new {
Key = account.Key,
// Property to see whether a booth number should be displayed
HasBooth = !String.IsNullOrWhiteSpace(account.Key.Booth),
HasAssignedDigitalLines = account.ProductOrders.Any(order => order.AssignedLines.Any(line => line.Type == "digital")),
// Dividing the orders into their respective product group
PhoneOrders = account.ProductOrders.Where(prod => ProductCodes.PHONE_CODES.Contains(prod.Key.ProductCode)),
InternetOrders = account.ProductOrders.Where(prod => ProductCodes.INTERNET_CODES.Contains(prod.Key.ProductCode)),
AdditionalOrders = account.ProductOrders.Where(prod => ProductCodes.ADDITIONAL_CODES.Contains(prod.Key.ProductCode))
})
.ToList();
我使用添加的属性来帮助设置输出样式。例如,我使用 HasBooth 属性检查是否应该在参展商名称旁边的括号中输出展位位置。问题是我必须将这个大查询保存为 IEnumerable,这意味着我收到错误消息:无法使用 lambda 表达式作为动态分派操作的参数,而无需先将其转换为委托或表达式树类型。我什至应该以这种方式操作查询吗?
非常感谢任何建议!