LINQ表达式节点类型'Invoke'在LINQ到实体中不支持

本文关键字:LINQ 实体 不支持 节点 表达式 Invoke 类型 | 更新日期: 2023-09-27 18:10:48

我有一个问题,试图实现一个过滤表达式来过滤实体列表:

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或任何额外的扩展库),我可以使这个工作?

我想使用表达式作为过滤器的原因是,最终,我想这样做:

var documentEntries = this.ObjectSet.Where(de => 
    dimensionMatches(de, currentVersionRateGroups, 1)
    && dimensionMatches(de, currentVersionRateGroups, 2)
    && dimensionMatches(de, currentVersionRateGroups, 3)
    && dimensionMatches(de, currentVersionRateGroups, 4));

另外,我怎样才能调试这种问题呢?错误信息非常模糊。如何查找导致错误的确切节点?

LINQ表达式节点类型'Invoke'在LINQ到实体中不支持

我怀疑这就是问题所在。

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));
        }
    }
}