Linq Grouping with Include

时间:2013-09-20 05:37:16

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

我有以下表格:Items,ItemMovements。

我想得到的是按ItemID分组的ItemMovements列表,其中每组的移动数量之和大于10.此外,我还想链接项目..

以下是我的最终结果:

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

1 个答案:

答案 0 :(得分:3)

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