实体框架 BuildContainsExpression 导致内部 .NET Framework 数据提供程序错误 1025。

10

我正在尝试使以下LINQ查询针对数据库(3.5 SP1)正常工作:

var labelIds = new List<int> { 1, 2 };
var customersAggregatedTransactionsByType =
    (from transactions in context.TransactionSet
    from customers in context.CustomerSet
        .Where(LinqTools.BuildContainsExpression<Billing.Customer, int>(u => u.LabelId, labelIds))
    from accounts in context.AccountSet
    where customers == accounts.Customer
        && accounts.Id == transactions.Account.Id
        && transactions.DateTime >= fromDate && transactions.DateTime < toDate
    group transactions.Amount
    by new
    {
        UserAccountId = transactions.Account.Id,
        TransactionTypeId = transactions.TransactionTypeId,
        BaseAssetId = accounts.BaseAssetId
    } into customerTransactions
    select customerTransactions).ToList();

当我添加了Where(LinqTools.BuildContainsExpression<Billing.Customer, int>(u => u.LabelId, labelIds))后,我会得到以下异常:

System.InvalidOperationException: 内部 .NET Framework 数据提供程序错误 1025。

如果我删除Where(LinqTools.BuildContainsExpression<Billing.Customer, int>(u => u.LabelId, labelIds))就没问题了。

希望能得到任何帮助。

谢谢,Nir。

1个回答

11

尝试:

        var labelIds = new List<int> { 1, 2 };
        var exp = LinqTools.BuildContainsExpression<Billing.Customer, int>(u => u.LabelId, labelIds);
        var customersAggregatedTransactionsByType =
             (from transactions in context.TransactionSet
              from customers in context.CustomerSet.Where(exp)
              from accounts in context.AccountSet
              where customers == accounts.Customer
                 && accounts.Id == transactions.Account.Id
                 && transactions.DateTime >= fromDate && transactions.DateTime < toDate
              group transactions.Amount
              by new
              {
                  UserAccountId = transactions.Account.Id,
                  TransactionTypeId = transactions.TransactionTypeId,
                  BaseAssetId = accounts.BaseAssetId
              } into customerTransactions
              select customerTransactions).ToList();
你想要查询结果,而不是对LinqTools.BuildContainsExpression方法本身的调用。

2
哦,你真是个天才!我在一个小时前开了一个类似的问题的悬赏,后来才发现了你的答案。请去那里领取你的悬赏吧。 - Shaul Behr
听起来很明显,但你是正确的,表达式必须在查询之外构建。 - Travis J

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接