2011-05-23 248 views
0

我有一個Linq到實體查詢與一個子查詢返回一個錯誤,我希望有人能夠幫助我。Linq到子查詢的實體查詢返回錯誤

錯誤消息:

"Unable to create a constant value of type 'SmallBusinessManager.Models.TransactionAllocation'. Only primitive types ('such as Int32, String, and Guid') are supported in this context." 

LINQ查詢:

var query = from transactionSelect in smallBusinessManagerDB.Transaction 
     select new 
     { 
     TransactionId = transactionSelect.TransactionId, 
     TransactionNumber = transactionSelect.TransactionNumber, 
     Amount = transactionSelect.Amount, 
     Balance = (from t2 in smallBusinessManagerDB.TransactionAllocation 
        where t2.InvoiceTransactionId == transactionSelect.TransactionId 
        group t2 by t2.TransactionAllocationId into g 
        select g.Sum(p => p.AllocatedAmount)) 
}; 

由於提前,

克里斯

+0

你爲什麼不使用關係? – SLaks 2011-05-23 02:31:39

+0

哪些對象或屬性的類型爲'TransactionAllocation'?它只是'smallBusinessManagerDB.TransactionAllocation'? – 2011-05-23 06:10:05

回答

0

試試這個:

var query = 
    from transactionSelect in smallBusinessManagerDB.Transaction 
    select new 
    { 
     TransactionId = transactionSelect.TransactionId, 
     TransactionNumber = transactionSelect.TransactionNumber, 
     Amount = transactionSelect.Amount, 
     Balance = smallBusinessManagerDB.TransactionAllocation 
      .Where(t2 => t2.InvoiceTransactionId == transactionSelect.TransactionId) 
      .Sum(p => p.AllocatedAmount) 
    }; 
+0

感謝Alex的迴應。但是,我做了更改,發生了同樣的錯誤。 無法創建類型爲「SmallBusinessManager.Models.TransactionAllocation」的常量值。在此上下文中僅支持基本類型(如Int32,String和Guid)。 – 2011-05-23 02:52:28

+0

如果你只是'var x = smallBusinessManagerDB.Transaction.ToList()'和'var y = smallBusinessManagerDB.TransactionAllocation.ToList()',會發生什麼? – 2011-05-23 02:58:22

+0

x以數據庫中的所有記錄返回爲System.Collection.Generic.List y返回爲System.Collection.Generic.List ,其中包含數據庫中的所有記錄 – 2011-05-23 03:05:15