سؤال

Here is my list of gamers and what games they like:

player1: WoW, RS, MS

player2: AQ, CoD, LoL, RS

player3: D3, Mario, WoW

player4: LoL, MS

The list property is public List<string> Games { get; set; }

Now this is what I've tried in my code

var result = Gamer.GetGamer()
                  .Select(x => x.Games)
                  .Distinct()
                  .Dump("List of Different Games Played");

The GetGamer() method returns the list of gamers.

Now the problem here is that it will display the list of games but will repeat instead of having different values.

What I really want is the total number of different games being played. How do I display them without repeats?

هل كانت مفيدة؟

المحلول

Use SelectMany instead of Select:

Gamer.GetGamer()
     .SelectMany(x => x.Games)
     .Distinct()
     .Dump();

If you want to get Count just add Count after Distinct

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top