代码之家  ›  专栏  ›  技术社区  ›  David Létourneau

如何使用LINQ-ES2015按两个键分组?

  •  1
  • David Létourneau  · 技术社区  · 6 年前

    简短问题:如何在Angular应用程序中使用LINQ-ES2015按两个键分组?

    一把钥匙很好用,但我不知道怎么加另一把钥匙。

    var results = asEnumerable(fieldtoregion)
          .GroupBy(x => x.regionId, x => x, (key, b) => {
            return { regionId: key, fields: asEnumerable(b).ToArray() }
          }).ToArray();
    

    第二个键名:iscollection。 我试过:

    1. x=>{r:x.regionid,c:x.iscollection}
    2. x=>({r:x.regionid,c:x.iscollection})
    3. x=>(“$x.regionid$x.iscollection”)

    任何帮助都是伟大的!啊!

    下面是要测试的typescript类:

    export class FieldViewModel {
      id!: number;
      postId!: number;
      pageId!: number;
      regionId?: string | undefined;
      translationId!: number;
      clrType?: string | undefined;
      fieldId?: string | undefined;
      isCollection!: boolean;
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Nina Scholz    6 年前

    您可以尝试仅使用第三和第四参数进行分组。分组字符串由两个带空格的属性组成。

    var results = asEnumerable(fieldtoregion)
            .GroupBy(
                null,
                null,
                (key, b) => ({ regionId: key, fields: asEnumerable(b).ToArray() }),
                "$.regionId + ' ' + $.isCollection"
            )
            .ToArray();