Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# linq GroupBy on the values of a List within a List

I have a List of Objects and within the Object there is a List of strings. What I want to do is find out how many of each string value there are.

So to create a simple example with the languages spoken by people in a team.

public class PeopleLanguages
{
    public string Name;
    public List<string> Languages;
}

Create the test data...

List<PeopleLanguages> peopleLanguages = new List<PeopleLanguages>();

peopleLanguages.Add(new PeopleLanguages { Name = "Rod", Languages = new List<string> { "English", "French", "German" } });
peopleLanguages.Add(new PeopleLanguages { Name = "Jane", Languages = new List<string> { "English", "Spanish", "Greek" } });
peopleLanguages.Add(new PeopleLanguages { Name = "Fredie", Languages = new List<string> { "French", "Arabic", "Italian" } });
peopleLanguages.Add(new PeopleLanguages { Name = "Viktor", Languages = new List<string> { "English", "Krakozhian" } });

To visualise the data:

 * Rod => English | French | German
 * Jane => English | Spanish | Greek
 * Fredie => French | Arabic | Italian
 * Viktor => English | Krakozhian

I can get the result I want by finding the distinct string values using SelectMany().Distinct() then matching within a foreach loop:

foreach (string language in peopleLanguages.SelectMany(p => p.Languages).Distinct())
{
    Console.WriteLine($"{language} = {peopleLanguages.Where(p => p.Languages.Contains(language)).Count()}");
}

Output:

English = 3
French = 2
German = 1
Spanish = 1
Greek = 1
Arabic = 1
Italian = 1
Krakozhian = 1

But there has to be a better way to do this using GroupBy().

I'm just stuck about how to get the individual distinct values out of the List of languages.

like image 579
Robert Pilcher Avatar asked Feb 12 '20 09:02

Robert Pilcher


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

Demo on dotnet fiddle

You can use SelectMany to get a list of list langues, then GroupBy as you wish like below

var result = peopleLanguages
    .SelectMany(p => p.Languages)
    .GroupBy(p => p);
    
foreach(var item in result)
{
    Console.WriteLine($"{item.Key} : {item.Count()}");
}

Output

English : 3
French : 2
German : 1
Spanish : 1
Greek : 1
Arabic : 1
Italian : 1
Krakozhian : 1
like image 90
Nguyễn Văn Phong Avatar answered Sep 24 '22 06:09

Nguyễn Văn Phong