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 :)