我正在编写一个ASP.NET Web Pages应用程序,在其中,我有一个庞大的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表达式用作动态调度操作的参数,而无需先将其转换为委托或表达式树类型。我是否应该以这种方式操纵查询?
非常感谢任何建议!
答案 0 :(得分:0)
在某些时候,您将动态数据类型传递给方法,而该方法又将返回类型更改为dynamic
。您可以将动态类型转换为在编译时识别的类型,也可以显式设置返回类型,而不是使用var
。
您可以在此处详细了解此问题:http://www.mikesdotnetting.com/Article/198/Cannot-use-a-lambda-expression-as-an-argument-to-a-dynamically-dispatched-operation