-3

Let’s suppose I have a model class like

class Player
{
    String FirstName;
    String LastName;
    String Team;
    int UniformNumber;
    int Height;
}

Now I have a list of this class: List Players;

This list contains some instances, e.g.,

Players[0] = new Player(){
    FirstName = "Kobe",
    LastName = "Byrant",
    Team = "Lakers",
    UniformNumber = 24,
    Height = 19}; //...

Is there a way I can get a sub List<Player> type list, which contains same Height and same UniformNumber players?

Can someone show me a way to do it quickly(suppose there are 10 000 players in the list)? Does LINQ fast enought? Thanks.

[Edit] Before asking the question, I use below codes:

var subList = new List<Player>();
        foreach (var player in players)
        {
            if (players.Where(p =>
                (p.Height == player.Height) &&
                (p.UniformNumber  == player.UniformNumber )).Count() > 1)
            {
                subList.Add(item);
            }
        }

I can get the result, but it is really slow, so i want the suggestion, thanks.

  • 4
    "Does LINQ fast enough" - well how fast do you need it to be? Have you *tried* using LINQ yet? – Jon Skeet Nov 21 '16 at 07:36
  • 1
    LINQ will not give you any performance boost – mybirthname Nov 21 '16 at 07:36
  • Where did you copy that text from? Your quotes were all wrong. – Manfred Radlwimmer Nov 21 '16 at 07:37
  • *“to do it quickly”* – In order to group all players, you will have to look at every single one of them. That’s the minimum amount of work you need to do. Is that already too slow for you? Then there’s no way *“to do it quickly”*. What “slow” solutions have you tried? – poke Nov 21 '16 at 07:37
  • It's amazing how many people just spew LQ answers and delete then once they realize they have no idea what the question is ... – Manfred Radlwimmer Nov 21 '16 at 07:41
  • @ManfredRadlwimmer, it is a piece of fake code, i didn't copy them from the real project. – user2987339 Nov 21 '16 at 07:45
  • @poke, you are right, now I add my code, it is really very slow, so i need your suggestion, thanks. – user2987339 Nov 21 '16 at 07:45
  • @JonSkeet, i just attached my code, it really not fast, please have a look and if possible, give me some suggestion, thanks. – user2987339 Nov 21 '16 at 07:49
  • Do you actually want to group the players, or do you just want to filter those that share their height and uniform number with at least one other player? Because that’s what your code does. – poke Nov 21 '16 at 08:04
  • @poke, if there are players have same height&uniformnumber, then I put the players into the new list – user2987339 Nov 21 '16 at 08:46

1 Answers1

0

First you need to make the properties public:

class Player
{
    public String FirstName {get;set}
    public String LastName {get;set;}
    public String Team {get;set;}
    public int UniformNumber {get;set;}
    public int Height {get;set}
}

Then you can write a group by query like this:

var result= ls
   .GroupBy (l =>new{l.UniformNumber,l.Height})
   .Select (l =>    
  new{l.Key.Height,l.Key.UniformNumber,Players=l.Select(s=>s).ToList}).ToList();
Arion
  • 31,011
  • 10
  • 70
  • 88