Question

 List<object> Students = new List<object>() {
    new { Name = "A", Age = 28, Height = 175 },
    new { Name = "B", Age = 29, Height = 176 },
    new { Name = "C", Age = 30, Height = 177 },
    new { Name = "D", Age = 31, Height = 178 },
    new { Name = "A", Age = 32, Height = 179 },
    new { Name = "E", Age = 33, Height = 180 },
    new { Name = "A", Age = 34, Height = 181 },
    new { Name = "F", Age = 35, Height = 182 },
    new { Name = "B", Age = 36, Height = 183 }
};

1) how can I group the above list by Age?

I tried something like var test = Students.GroupBy(x=> x.Age);, but it didn't work.

2) I want to create a dictionary which has Name as Key and Height as Value . How can I do this?

Was it helpful?

Solution

Your approach to use straight object wont work because object does not have Age property. It is better for you to use predefined class with Name, Age and Height properties. It will be much more readable and type safe...

class Student
{
   public string Name {get; set;}
   public int Age {get; set;}
   public int Height {get; set;}

   public Student() {} //you would need parameterless constructor to use "new Student {...}"
   public Student(string name, int age, int height)
   {
      Name = name;
      Age = age;
      Height = height;
   }
}
var Students = new List<Student>() 
                   { 
                      new Student { Name = "A", Age = 29, Height = 175 },  //using initializer
                      new Student("B", 30, 176), //using constructor
                      ... 
                   };

But if it is impossible - you can always use dynamic. Like this:

List<dynamic> Students = new List<dynamic>() 
                             { 
                                new { Name = "A", Age = 28, Height = 175 }, 
                                ...
                             };
var test = Students.GroupBy(x=> x.Age);

and for dictionary - use ToDictionary:

var test = Students.ToDictionary(x=> x.Name, x => x.Height);

however you might want to try ToLookup because you have duplicates in your Names :)

OTHER TIPS

You can do the following:

 var Students = new []{
    new { Name = "A", Age = 28, Height = 175 },
    new { Name = "B", Age = 29, Height = 176 },
    new { Name = "C", Age = 30, Height = 177 },
    new { Name = "D", Age = 31, Height = 178 },
    new { Name = "A", Age = 32, Height = 179 },
    new { Name = "E", Age = 33, Height = 180 },
    new { Name = "A", Age = 34, Height = 181 },
    new { Name = "F", Age = 35, Height = 182 },
    new { Name = "B", Age = 36, Height = 183 }
}.ToList();

var groupedStudents = Students.GroupBy(x=>x.Age);

The key differences are that I create an anonymous list (I don't actually need the .ToList() but I'm showing you how in case you want to do any other List stuff with it). The advantage of this is that ToList is a generic method that can infer what the type is from the enumerable it is working on which in this case is an enumerable of anonymous types.

Of course many times you might be better off just creating a Student object rather than using an anonymous one.

As for creating a dictionary with name as key and height as value - you can't. Dictionaries need to have unique keys and you have multiple items with the same name here so trying to do ToDictionary() it wouldn't work.

You could try using ToLookup() which would take a key to an enumerable of matching values (similar to group by but easier to look things up).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top