C# Mvc从每个类别中挑选1项

C# Mvc从每个类别中挑选1项,c#,C#,假设我有一个此类的对象列表: public class CategoryForHome { public string Name { get; set; } public string Img { get; set; } public string Category { get; set; } } 许多对象都有相同的类别,但我感兴趣的是创建一个只包含1个对象/类别的新列表。我想不出这是怎么做到的? 使用distinct()?使用T

假设我有一个此类的对象列表:

public class CategoryForHome
    {
        public string Name { get; set; }
        public string Img { get; set; }
        public string Category { get; set; }
    }
许多对象都有相同的类别,但我感兴趣的是创建一个只包含1个对象/类别的新列表。我想不出这是怎么做到的? 使用distinct()?使用Take()

Var newList=newList()
Foreach(列表中的变量项)
{
//做点什么
newList.Add(项目)
}
我希望我清楚我想要达到的目标,感谢你的帮助。 塔恩克斯

Var newList = new List<CategoryForHome>()

Foreach(var item in list)
{
 //Do something
   newList.Add(item)
}
foreach (var item in 
                list.GroupBy(catForHome => catForHome.Category)
                    .Select(group => group.OrderBy(catForHome => catForHome.Name).First()))
{
    // got item with the lowest name in every category
}