代码之家  ›  专栏  ›  技术社区  ›  Mike Fielden

LINQ查询问题

  •  2
  • Mike Fielden  · 技术社区  · 14 年前

    我有以下信息

    var details = Details.Where(d => d.isActive);
    

    我想查询另一个表, Authorizations ,它有一个 Details ,并获取包含在 details 对象分组依据 detail 和A FundCode

    详细(一对多)授权

    看起来很简单,但是我有点麻烦。

    以下是我目前拥有的:

    var account = (from sumOfAuths in Authorizations
                   where details.Contains(sumOfAuths.Details)
                         && sumOfAuths.RequestStatusId == 2
                   group sumOfAuths by new { sumOfAuths.Detail, sumOfAuths.FundCode } into child
                   select new { 
                    ....
                    Amount = child.Amount 
                   }).Sum()
    

    问题是 .Sum() 函数我有对象集合而不是1。所以我无法正确地计算出总数。

    2 回复  |  直到 14 年前
        1
  •  2
  •   Sorax    14 年前

    我相信这个查询会生成您要查找的内容:

                var account = from c in Authorizations
                          where details.Contains(c.Details) && c.RequestStatusId == 2
                          group c by new { c.Detail, c.FundCode } into g
                          select new { Key = g.Key, Sum = g.Sum(x => x.Amount) };
    
        2
  •  3
  •   David Hedlund    14 年前

    通常,可以指定要求和的值:

    .Sum(x => x.Amount)
    

    在组中,可以使用嵌套和:

    .Sum(x => x.Sum(y => y.Amount));