2010-10-03 27 views
9

Tôi đang cố gắng để có được những truy vấn LINQ sau đây để làm việc chống lại cơ sở dữ liệu (3.5 SP1):Entity Framework BuildContainsExpression Nguyên nhân nội .NET Framework cung cấp dữ liệu lỗi 1025

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

Khi tôi thêm Where(LinqTools.BuildContainsExpression<Billing.Customer, int>(u => u.LabelId, labelIds)) tôi nhận được như sau ngoại lệ:

System.InvalidOperationException: Internal .NET Framework cung cấp dữ liệu lỗi 1025.

Nếu tôi loại bỏ Where(LinqTools.BuildContainsExpression<Billing.Customer, int>(u => u.LabelId, labelIds)) tất cả là tốt.

Mọi trợ giúp sẽ được đánh giá cao.

Cảm ơn, Nir.

Trả lời

11

Hãy thử:

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

Bạn muốn kết quả trong truy vấn, không phải là lời kêu gọi LinqTools.BuildContainsExpression riêng của mình.

+0

Điều đó xảy ra! Cảm ơn rất nhiều! – nirpi

+2

Ồ, bạn thiên tài! Tôi đã mở một khoản tiền thưởng cho [câu hỏi tương tự] (http://stackoverflow.com/q/11990158/7850) chỉ một giờ trước, và chỉ sau đó mới tìm được câu trả lời của bạn. Hãy đi và nhận cho mình một tiền thưởng ở đó. –

+0

Nghe có vẻ hiển nhiên, nhưng bạn đúng, biểu thức phải được xây dựng bên ngoài chính truy vấn đó. –

Các vấn đề liên quan