C# ll在GroupBy子句中。但是,我无法将其从第28行的Select子句中删除。这就是为什么编译到处都是。我的方法签名有问题吗?@MatthewYoung不编译任何表达式是非常重要的。通过将它们编译为委托,您可以将代码强制转换为LINQ to对象。您需要它
C# ll在GroupBy子句中。但是,我无法将其从第28行的Select子句中删除。这就是为什么编译到处都是。我的方法签名有问题吗?@MatthewYoung不编译任何表达式是非常重要的。通过将它们编译为委托,您可以将代码强制转换为LINQ to对象。您需要它,c#,performance,linq,lambda,expression-trees,C#,Performance,Linq,Lambda,Expression Trees,ll在GroupBy子句中。但是,我无法将其从第28行的Select子句中删除。这就是为什么编译到处都是。我的方法签名有问题吗?@MatthewYoung不编译任何表达式是非常重要的。通过将它们编译为委托,您可以将代码强制转换为LINQ to对象。您需要它来使用EF,并将其转换为SQL代码。如果你编译这些表达式,就不会发生这种情况。我想你已经找到了根本问题。从组中删除Compile()时出错,因为LINQ to Entities无法识别我的dictionary对象,并且该方法无法转换为存储表达式
ll在GroupBy子句中。但是,我无法将其从第28行的Select子句中删除。这就是为什么编译到处都是。我的方法签名有问题吗?@MatthewYoung不编译任何表达式是非常重要的。通过将它们编译为委托,您可以将代码强制转换为LINQ to对象。您需要它来使用EF,并将其转换为SQL代码。如果你编译这些表达式,就不会发生这种情况。我想你已经找到了根本问题。从组中删除Compile()时出错,因为LINQ to Entities无法识别我的dictionary对象,并且该方法无法转换为存储表达式。当我说我“无法”从第28行删除Compile调用时,编译器只是说GetAveragePartySizeSelector()在Select子句中无效。@MatthewYoung那么您需要找出原因。在这种情况下,错误消息会非常清楚地说明这一点。您没有
IQueryable
,您有一个IEnumerable
,这意味着您正在更早地将查询转换为内存序列。您的方法使用的是IEnumerable
,而不是IQueryable
,因此确保所有操作都作用于内存中的集合。
var query = ctx.Respondents
.Join(
ctx.Respondents,
other => other.RespondentId,
res => res.RespondentId,
(other, res) => new ChartJoin { Respondent = res, Occasion = null, BrandVisited = null, BrandInfo = null, Party = null, Item = null }
)
. // bunch of other joins filling out the ChartJoin
.Where(x => x.Respondent.status == 1)
. // more Where clauses dynamically applied
.GroupBy(x => new CommonGroupBy { Year = (int)x.Respondent.currentVisitYear, Month = (int)x.Respondent.currentVisitMonth })
.OrderBy(x => x.Key.Year)
.ThenBy(x => x.Key.Month)
.Select(x => new AverageEaterCheque
{
Year = x.Key.Year,
Month = x.Key.Month,
AverageCheque = (double)(x.Sum(m => m.BrandVisited.DOLLAR_TOTAL) / x.Sum(m => m.BrandVisited.NUM_PAID)),
Base = x.Count(),
Days = x.Select(m => m.Respondent.visitDate).Distinct().Count()
});
public static Expression<Func<IGrouping<IDictionary<string, object>, ChartJoin>, AverageEaterCheque>> GetAverageEaterChequeSelector()
{
// x =>
var ParameterType = typeof(IGrouping<IDictionary<string, object>, ChartJoin>);
var parameter = Expression.Parameter(ParameterType);
// x => x.Sum(m => m.BrandVisited.DOLLAR_TOTAL) / x.Sum(m => m.BrandVisited.NUM_PAID)
var m = Expression.Parameter(typeof(ChartJoin), "m");
var mBrandVisited = Expression.PropertyOrField(m, "BrandVisited");
PropertyInfo DollarTotalPropertyInfo = typeof(BrandVisited).GetProperty("DOLLAR_TOTAL");
PropertyInfo NumPaidPropertyInfo = typeof(BrandVisited).GetProperty("NUM_PAID");
....
return a lambda...
}