实际上,我正在尝试按城市对学生列表进行分组。当我执行此操作时,我在 s2.City 附近的 LINQ 语句中出现“对象引用”错误。
class Groupby
{
Student[] s = new Student[10];
public Groupby()
{
s[0] = new Student("Manikandan", "Jayagopi", "Chennai");
s[1] = new Student("Ganesh", "Bayyanna", "Bangalore");
s[2] = new Student("Laxman", "Bharghav", "Hyderabad");
s[3] = new Student("Dinesh","Vellaiyan","Pune");
s[4] = new Student("Natarajan","Nallathambi","Chennai");
}
public void Group()
{
var groupQuery = from s2 in s
group s2 by s2.City;
foreach (Student s1 in groupQuery)
Console.WriteLine(" {0}", s1.FirstName);
}
}
class Program
{
static void Main()
{
Groupby objGroupby = new Groupby();
objGroupby.Group();
Console.ReadLine();
}
}
谁能帮我吗?
提前致谢