C# Linq到实体查询,子查询返回错误
我有一个Linq到实体的查询,其中有一个子查询返回一个错误,我希望有人能帮我解决 错误消息:C# Linq到实体查询,子查询返回错误,c#,linq-to-sql,linq-to-entities,C#,Linq To Sql,Linq To Entities,我有一个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
"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))
};
提前感谢,
克里斯试试这个:
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)
};
为什么不使用关系?哪些对象或属性的类型为
TransactionAllocation
?这只是smallBusinessManagerDB.TransactionAllocation
?感谢您的回复。我做了改变,但是,同样的错误发生了。无法创建“SmallBusinessManager.Models.TransactionAllocation”类型的常量值。在此上下文中仅支持基本类型(“如Int32、字符串和Guid”)。如果只执行var x=smallBusinessManagerDB.Transaction.ToList()
和var y=smallBusinessManagerDB.TransactionAllocation.ToList(),会发生什么情况
?x以System.Collection.Generic.List的形式返回数据库中的所有记录,y以System.Collection.Generic.List的形式返回数据库中的所有记录database@ChrisNieu-你能接受我的查询,评论余额设置器并检查它是否有效吗?(试图将此问题本地化)