Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a list of the grouped values in linq groupby?

Tags:

linq

group-by

Linq newbie here, struggling with my first group by query.

I have a list of objects of type KeywordInstance which represent a keyword, and the ID of the database record to which the keyword was applied.

Keyword      RecordID
macrophages  1
macrophages  2
cell cycle   3 
map kinase   2
cell cycle   1

What I want to have is a collection of all keywords, with a list of the RecordIDs to which each keyword was applied

Keyword     RecordIDs 
macrophages 1, 2
cell cycle  1, 3
map kinase  2

I tried using Linq to get it into a new object. I only managed to get the distinct keywords.

var keywords = allWords .GroupBy(w => w.keyword) .Select(g => new {keyword = g.Key});

The problem is that I can't seem to get the values of g in any way. g is of the type IGrouping and by documentation, it only has the property Key, but not even the property Value. All the examples I have seen on the Internet for groupby just tell me to select g itself, but the result of

var keywords = allWords
    .GroupBy(w => w.keyword)
    .Select(g => new {keyword = g.Key, RecordIDs = g});

is not what I want.

screenshot of result

Any try to get something out of g fails with the error message System.Linq.IGropuing<string, UserQuery.KeywordInstance> does not have a definition for [whatever I tried].

What am I doing wrong here?

like image 650
Rumi P. Avatar asked Nov 06 '13 13:11

Rumi P.


People also ask

What does LINQ GroupBy return?

GroupBy & ToLookup return a collection that has a key and an inner collection based on a key field value. The execution of GroupBy is deferred whereas that of ToLookup is immediate. A LINQ query syntax can be end with the GroupBy or Select clause.

How does GroupBy work LINQ?

The working of the GroupBy operator is similar to the SQL GroupBy clause. It is used to return the group of elements which share the common attributes or key from the given sequence or collection. Every group is represented by IGrouping<TKey, TElement> object.

Does LINQ GroupBy keep order?

Found answer on MSDN: Yes. The IGrouping<TKey, TElement> objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping<TKey, TElement> . Elements in a grouping are yielded in the order they appear in source.

What is GroupBy C#?

GroupBy() Method in C# The GroupBy() is an extension method that returns a group of elements from the given collection based on some key value. arr. GroupBy(b => chkSmaller(b)); The above chkSmaller() finds the elements smaller than 50.


1 Answers

I think you are close to you solution.

var keywords = allWords
    .GroupBy(w => w.keyword)
    .Select(g => new 
           { 
              keyword = g.Key, 
              RecordIDs = g.Select(c => c.ID)
           });

Just Select the records you need.
The reason you are seeing the Keyword-column as well as the ID-column, is becuase it's part of g

like image 124
Jens Kloster Avatar answered Oct 19 '22 09:10

Jens Kloster