【问题标题】:Entity Framework BuildContainsExpression Causes Internal .NET Framework Data Provider error 1025实体框架 BuildContainsExpression 导致内部 .NET Framework 数据提供程序错误 1025
【发布时间】:2010-10-03 16:46:54
【问题描述】:

我正在尝试让以下 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&lt;Billing.Customer, int&gt;(u =&gt; u.LabelId, labelIds)) 后,我得到以下异常:

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

如果我删除 Where(LinqTools.BuildContainsExpression&lt;Billing.Customer, int&gt;(u =&gt; u.LabelId, labelIds)) 一切都很好。

任何帮助将不胜感激。

谢谢,尼尔。

【问题讨论】:

    标签: entity-framework internal


    【解决方案1】:

    试试:

            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 本身的调用。

    【讨论】:

    • 哦,你这个天才!就在一小时前,我在similar question 上开了一个赏金,后来才找到你的答案。请去那边给自己一个赏金。
    • 听起来很明显,但你是对的,表达式必须在查询本身之外构建。
    猜你喜欢
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 2017-04-26
    相关资源
    最近更新 更多