[英]EF Core LINQ query failing due to limitation?
I am trying to do quite a simple group by, and sum, with EF Core 3.0我正在尝试使用 EF Core 3.0 进行相当简单的分组和汇总
However am getting a strange error:但是我收到一个奇怪的错误:
System.InvalidOperationException: 'Processing of the LINQ expression 'AsQueryable((Unhandled parameter: y).TransactionLines)' by 'NavigationExpandingExpressionVisitor' failed.
System.InvalidOperationException:“NavigationExpandingExpressionVisitor”处理 LINQ 表达式“AsQueryable((未处理的参数:y).TransactionLines)”失败。 This may indicate either a bug or a limitation in EF Core.
这可能表示 EF Core 中的错误或限制。
var creditBalances = await context.Transaction
.Include(x => x.TransactionLines)
.Include(x=>x.CreditAccount)
.Where(x => x.CreditAccount.UserAccount.Id == userAccount.Id)
.GroupBy(x => new
{
x.CreditAccount.ExternalId
})
.Select(x => new
{
x.Key.ExternalId,
amount = x.Sum(y => y.TransactionLines.Sum(z => z.Amount))
})
.ToListAsync();
I'm battling to see where an issue can arise, so not even sure where to start.我正在努力寻找可能出现问题的地方,所以甚至不知道从哪里开始。 I am trying to get a sum of all the transaction amounts (Which is a Sum of all the TransactionLines for each transaction - ie A Transaction amount is made of the lines associated to it).
我试图获得所有交易金额的总和(这是每笔交易的所有 TransactionLines 的总和 - 即交易金额由与其关联的行组成)。
I then sum up all the transactions, grouping by then CreditAccount ID.然后我总结所有交易,然后按 CreditAccount ID 分组。
The line, Unhandled parameter: y
is worrying. Unhandled parameter: y
这行令人担忧。 Maybe my grouping and summing is out.也许我的分组和求和已经出来了。
So start at the TransactionLines
level and this is as simple as:所以从
TransactionLines
级别开始,这很简单:
var q = from c in context.TransactionLines
where c.Transaction.CreditAccount.UserAccount.Id == userAccount.Id
group c by c.Transaction.CreditAccount.ExternalId into g
select new
{
ExternalId = g.Key,
Amount = g.Sum(x => x.Amount)
};
var creditBalances = await q.ToListAsync();
( You don't need any Include()
since you're not returning an Entity with related data. You're projecting a custom data shape. ) (您不需要任何
Include()
因为您没有返回具有相关数据的实体。您正在投影自定义数据形状。)
Which translates to:翻译为:
SELECT [c].[ExternalId], SUM([t].[Amount]) AS [Amount]
FROM [TransactionLines] AS [t]
LEFT JOIN [Transaction] AS [t0] ON [t].[TransactionId] = [t0].[Id]
LEFT JOIN [CreditAccounts] AS [c] ON [t0].[CreditAccountId] = [c].[Id]
LEFT JOIN [UserAccount] AS [u] ON [c].[UserAccountId] = [u].[Id]
WHERE [u].[Id] = @__userAccount_Id_0
GROUP BY [c].[ExternalId]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.