LINQ Select distinct from a List?

前端 未结 3 732
不知归路
不知归路 2021-01-18 14:41

I have the following list:


    class Person
    {
        public String Name { get; set; }
        public String LastName { get; set; }
        public Stri         


        
3条回答
  •  [愿得一人]
    2021-01-18 15:14

    You could use LINQ to group the persons list by city:

    var groupedPersons = personList.GroupBy(x => x.City);
    foreach (var g in groupedPersons)
    {
        string city = g.Key;
        Console.WriteLine(city);
        foreach (var person in g)
        {
            Console.WriteLine("{0} {1}", person.Name, person.LastName);
        }
    }
    

提交回复
热议问题