考虑以下Linq to Entities查询:
return (from lead in db.Leads
join postcodeEnProvincie in postcodeEnProvincies
on lead.Postcode equals postcodeEnProvincie.Postcode
where (lead.CreationDate >= range.StartDate) && (lead.CreationDate <= range.EndDate)
group lead by postcodeEnProvincie.Provincie into g
select new Web.Models.GroupedLeads() {
GroupName = g.Key,
HotLeads = g.Count(l => l.Type == Data.LeadType.Hot),
Leads = g.Count(),
PriorityLeads = g.Count(l => l.Type == Data.LeadType.Priority),
Sales = g.Count(l => l.Sold),
ProductA = g.Count(l => l.Producten.Any(a => ((a.Name.Equals("productA", StringComparison.CurrentCultureIgnoreCase)) || (a.Parent.Name.Equals("productA", StringComparison.CurrentCultureIgnoreCase))))),
ProductB = g.Count(l => l.Producten.Any(a => ((a.Name.Equals("productB", StringComparison.CurrentCultureIgnoreCase)) || (a.Parent.Name.Equals("productB", StringComparison.CurrentCultureIgnoreCase))))),
ProductC = g.Count(l => l.Producten.Any(a => ((a.Name.Equals("productC", StringComparison.CurrentCultureIgnoreCase)) || (a.Parent.Name.Equals("productC", StringComparison.CurrentCultureIgnoreCase))))),
ProductC = g.Count(l => l.Producten.Any(a => ((a.Name.Equals("productD", StringComparison.CurrentCultureIgnoreCase)) || (a.Parent.Name.Equals("productD", StringComparison.CurrentCultureIgnoreCase)))))
}).ToList();
如果你像我一样,你的脚趾会重复产品选择逻辑。这种模式也在另一个地方重复。我首先尝试用IEnumerable上的扩展方法替换它,这当然不起作用:Linq to Entities需要一个Expression来解析和翻译。
所以我创建了这个方法:
public static System.Linq.Expressions.Expression<Func<Data.Lead, bool>> ContainingProductEx(string productName)
{
var ignoreCase = StringComparison.CurrentCultureIgnoreCase;
return (Data.Lead lead) =>
lead.Producten.Any(
(product =>
product.Name.Equals(productName, ignoreCase) ||
product.Parent.Name.Equals(productName, ignoreCase)
));
}
以下选择现在完全正常:
var test = db.Leads.Where(Extensions.ContainingProductEx("productA")).ToList();
然而,这不会编译,因为IGrouping不包含Where接受表达式的覆盖:
return (from lead in db.Leads
join postcodeEnProvincie in postcodeEnProvincies
on lead.Postcode equals postcodeEnProvincie.Postcode
where (lead.CreationDate >= range.StartDate) && (lead.CreationDate <= range.EndDate)
group lead by postcodeEnProvincie.Provincie into g
select new Web.Models.GroupedLeads()
{
GroupName = g.Key,
HotLeads = g
.Where(l => l.Type == Data.LeadType.Hot)
.Count(),
Leads = g.Count(),
PriorityLeads = g
.Where(l => l.Type == Data.LeadType.Priority)
.Count(),
Sales = g
.Where(l => l.Sold)
.Count(),
ProductA = g
.Where(Extensions.ContainingProductEx("productA"))
.Count(),
ProductB = g
.Where(Extensions.ContainingProductEx("productB"))
.Count(),
ProductC = g
.Where(Extensions.ContainingProductEx("productC"))
.Count(),
ProductD = g
.Where(Extensions.ContainingProductEx("productD"))
.Count()
}).ToList();
将g转换为IQueryable编译,但随后产生一个&#34;内部.NET Framework数据提供程序错误1025。&#34;。
有没有办法将这个逻辑包装在自己的方法中?
答案 0 :(得分:2)
这是一个可以使用LINQKit解决的问题。它允许从其他表达式中调用表达式,并在其调用者中内联调用的表达式。遗憾的是,它只支持一些非常具体的情况,所以我们需要稍微调整你的表达式生成方法。
我们不是将产品名称传递给表达式生成方法,而是将它作为返回表达式的参数:
public static Expression<Func<Data.Lead, string, bool>> ContainingProductEx()
{
var ignoreCase = StringComparison.CurrentCultureIgnoreCase;
return (lead, productName) =>
lead.Producten.Any(
(product =>
product.Name.Equals(productName, ignoreCase) ||
product.Parent.Name.Equals(productName, ignoreCase)
));
}
接下来我们需要在声明查询之前调用该方法:
var predicate = Extensions.ContainingProductEx();
您的查询现在可以写成:
from lead in db.Leads.AsExpandable()
//...
ProductA = g
.Where(lead => predicate.Invoke(lead, "productA"))
.Count(),
ProductB = g
.Where(lead => predicate.Invoke(lead, "productB"))
.Count(),
ProductC = g
.Where(lead => predicate.Invoke(lead, "productC"))
.Count(),
ProductD = g
.Where(lead => predicate.Invoke(lead, "productD"))
.Count()
答案 1 :(得分:-1)
为什么不创建一个单独的私有方法来获取IEnumerable<Lead>
,一个字符串,并返回一个int并在您的查询中引用方法组?我认为您的困惑源于尝试在集合上创建扩展方法,而不是创建集合中的方法以及您正在寻找的值。
类似的东西:
ProductA = GetLeadsForProduct(g, "productA")
private int GetLeadsForProduct(IEnumerable<Lead> leads, string productType)
{
return leads.Count(l => l.Producten.Any(a => ((a.Name.Equals(productType, StringComparison.CurrentCultureIgnoreCase)) || (a.Parent.Name.Equals(productType, StringComparison.CurrentCultureIgnoreCase)))))
}