Alan2
Alan2

Reputation: 24592

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:

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

But it's giving me this error:

enter image description here

Can anyone help and give me advice on what might be wrong?

Upvotes: 0

Views: 41

Answers (1)

spectacularbob
spectacularbob

Reputation: 3228

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

Upvotes: 4

Related Questions