0

我在尝试实现过滤表达式来过滤实体列表时遇到问题:

LINQ to Entities 不支持 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));

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

4

1 回答 1

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

}
于 2013-04-25T14:11:48.517 回答