c# - Entity Framework Count and Group By in a Collection -


i have lambda expression returns data filtered, need calculations on records returned.

i have 2 entities (user , language) have many-to-many relationship , need make grouped count of returnees languages find out how many people speak each returned language.

sample set of data returned lambda expression:

  • user1 (english, spanish)
  • user2 (english, french)
  • user3 (english, spanish)

desired result:

  • english 3
  • spanish 2
  • french 1

i tried use groupby () , select (), got right result foreach on results, using lambda.

[edit] solution key results flattening using selectmany () in combination groupby(). @christos point right answer.

you try this:

var results = data.selectmany(user=>user.languages)                   .groupby(language=>language)                   .select(gr=>new { language = gr.key, total = gr.count()}); 

i have supposed each user object has sequence of strings, ienumerable<string>, called languages , holds user's languages. furthermore, data have sequence of users.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -