c# - 使用 Include 进行 Linq 分组

标签 c# sql-server winforms linq entity-framework

我有以下表格:Items、ItemMovements。

我想要得到的是按 ItemID 分组的 ItemMovements 列表,其中每组的移动数量之和大于 10。另外我也想链接 Item..

这是我最终得到的结果:

    var res = ctx.ItemMovements
            .Include("Item")
            .GroupBy(x => x.ItemID)
            .ToList();

最佳答案

var idQuery = ctx.ItemMovements
    .GroupBy(e => e.ItemID)
    .Select(g => new { ItemID = g.Key, QuantitySum = g.Sum(Quantity) } )
    .Where(e => e.QuantitySum > 10)
    .Select(e => e.ItemID);

var query = ctx.ItemMovements
    .Include("Item")
    .Where(e => idQuery.Contains(e.ItemID));

关于c# - 使用 Include 进行 Linq 分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18909536/

相关文章:

sql-server - SQL Server Profiler 未显示嵌入在 ColdFusion 组件中的 SQL 语句

c# - 使用 PostgreSql 和 ADO.NET 在 C# WinForms 中锁定记录和表

c# - 尝试读取或写入 protected 内存。这通常表明其他内存已损坏。在 Windows Phone 8 中

sql - 为什么在SQL Server中像 `select last`一样没有 `select bottom`或 `select top`?

sql - 'IN'可以用在case语句中吗?

c# - 按钮启用属性无法正常工作

c# - 只有赋值调用自增自减才能作为语句

c# - 如何在 C# .NET 文档中添加换行符

c# - 从父子任务返回值

c# - 检查 URL 是否指向 Azure Blob