使用linq选择属性的第一个记录

Shy*_*mep 0 .net c# linq select

我上课了

class Test
{
    public string FirstProp { get; set; }
    public string SecondProp { get; set; }
    public string ThirdProp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

和一个对象列表

var list = new List<Test>
{
    new Test { FirstProp = "xxx", SecondProp = "x2", ThirdProp = "x3" },
    new Test { FirstProp = "xxx", SecondProp = "x21", ThirdProp = "x31" },
    new Test { FirstProp = "yyy", SecondProp = "y2", ThirdProp = "y3" },
    new Test { FirstProp = "yyy", SecondProp = "y21", ThirdProp = "y31" },
    new Test { FirstProp = "xxx", SecondProp = "x22", ThirdProp = "x32" },
};
Run Code Online (Sandbox Code Playgroud)

我需要选择第一个FirstProp记录:

FirstProp = "xxx", SecondProp = "x2", ThirdProp = "x3"
FirstProp = "yyy", SecondProp = "y2", ThirdProp = "y3"
Run Code Online (Sandbox Code Playgroud)

如何以最佳方式使用linq

cuo*_*gle 8

您可以GroupBy在酒店使用FirstProp,然后获得First:

 list.GroupBy(x => x.FirstProp).Select(g => g.First())
Run Code Online (Sandbox Code Playgroud)