简体   繁体   English

如何使用 GroupBy 将 select 转换为使用 LINQ 的匿名 class?

[英]How can I select with a GroupBy into an anonymous class with LINQ?

I have a list:我有一个清单:

public static List<PhraseSource> phraseSources;

The list has property:该列表具有以下属性:

public int? JishoJlpt     { get; set; }

I am trying to get a count of the number of how many of each number of JishJlpt occur:我正在尝试计算每个 JishJlpt 出现的次数:

phraseSources.GroupBy(p => p.JishoJlpt)
             .Select(g => new {
                JishoJlpt g.Key,
                Count: g.Count()
             }); 

But it's giving me this error:但它给了我这个错误:

在此处输入图像描述

Can anyone help and give me advice on what might be wrong?任何人都可以帮助我并就可能出现的问题提供建议吗?

Looks like a syntax error.看起来像语法错误。 Anonymous objects use = instead of :匿名对象使用=而不是:

This works for me:这对我有用:

List<int> list = new List<int>();
list.Add(1);
list.Add(1);
list.Add(3);

var items = list.GroupBy(p => p)
    .Select(g => new {
        Key = g.Key,
        Count = g.Count()
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM