我在尝试实施过滤表达式来过滤实体列表时遇到问题:
LINQ to不支持LINQ表达式节点类型'Invoke' 实体。
这是代码:
public IList<DocumentEntry> GetDocumentEntriesForRateAdjustmentTry2(
string username, Rate rate, List<RatePeriod> ratePeriods)
{
var dimensionLibManager = new DimensionLibManager();
var currentVersionRateGroups = rate.CurrentRateVersion.RateGroups.ToList();
Expression<Func<DocumentEntry, IList<RateGroup>, int, bool>> dimensionMatchesExpression =
(documentEntry, rateGroups, dimensionInfoId) =>
rateGroups.Any(
rg =>
rg.Dimension1.All(character => character == '*')
||
documentEntry.DocumentEntryDimensions.Any(
ded =>
ded.DimensionInfo.Position == dimensionInfoId
&&
dimensionLibManager.GetDimensionSegments(rate.CompanyId, username, dimensionInfoId, ded.Value).Any(
seg => ded.Value.Substring(seg.SegmentStart, seg.SegmentLength) == seg.SegmentValue)));
var dimensionMatches = dimensionMatchesExpression.Compile();
var documentEntries = this.ObjectSet.Where(de => dimensionMatches(de, currentVersionRateGroups, 1));
var result = documentEntries.ToList(); // The error happens here.
return result;
}
我怀疑 dimensionMatchesExpression 无法转换为SQL,因为它内部调用另一个库的方法( dimensionLibManager.GetDimensionSegments )来根据特定参数过滤文档。
有没有办法(除了使用LinqKit或任何额外的扩展库)我可以使这个工作?
我想使用Expression作为过滤器的原因是因为,最终,我想这样做:
var documentEntries = this.ObjectSet.Where(de =>
dimensionMatches(de, currentVersionRateGroups, 1)
&& dimensionMatches(de, currentVersionRateGroups, 2)
&& dimensionMatches(de, currentVersionRateGroups, 3)
&& dimensionMatches(de, currentVersionRateGroups, 4));
另外,我如何实际调试这类问题?错误消息非常模糊。如何找到导致错误的确切节点?
答案 0 :(得分:0)
我怀疑这是问题。
var documentEntries = this.ObjectSet.Where(de => dimensionMatches(de, currentVersionRateGroups, 1));
我不认为行号与Linq2EF一起使用。
public IList<DocumentEntry> GetDocumentEntriesForRateAdjustmentTry2(
string username, Rate rate, List<RatePeriod> ratePeriods)
{
var dimensionLibManager = new DimensionLibManager();
var currentVersionRateGroups = rate.CurrentRateVersion.RateGroups.ToList();
Expression<Func<DocumentEntry, int, bool>> dimensionMatchesExpression =
(documentEntry, rateGroups, dimensionInfoId) =>
currentVersionRateGroups.Any(
rg =>
rg.Dimension1.All(character => character == '*')
||
documentEntry.DocumentEntryDimensions.Any(
ded =>
ded.DimensionInfo.Position == dimensionInfoId
&&
dimensionLibManager.GetDimensionSegments(rate.CompanyId, username, dimensionInfoId, ded.Value).Any(
seg => ded.Value.Substring(seg.SegmentStart, seg.SegmentLength) == seg.SegmentValue)));
var documentEntries = this.ObjectSet.Where(dimensionMatchesExpression);
var result = documentEntries.ToList(); // The error happens here.
return result;
}
虽然我不明白为什么要使用表达式来执行此操作。你可以把它全部内联......
几天前刚刚意识到你的问题的解决方案......有点破解......
public Expression<Func<DocumentEntry, int, bool>> CreateWhereClause(stuff);
public IList<DocumentEntry> GetDocumentEntriesForRateAdjustmentTry2(
string username, Rate rate, List<RatePeriod> ratePeriods)
{
using(var db = new Context())
{
IQueryable<DocumentEntry> foo = db.Foos;
foreach(var i =0; i <4; i++)
{
foo = foo.Where(DocumentEntry(i));
}
}
}