且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

EF Core LINQ查询由于限制而失败?

更新时间:2023-02-15 17:12:10

所以从 TransactionLines 级别开始,这很简单:

So start at the TransactionLines level and this is as simple as:

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

(您不需要任何 Include(),因为您没有返回带有相关数据的实体.您正在投影自定义数据形状.)

( You don't need any Include() since you're not returning an Entity with related data. You're projecting a custom data shape. )

翻译为:

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]