猿问

元素的分组和排序

public class RebarUnique

{

    public Rebar MRebar { get; set; }

    public Point2d Point2d { get; set; }

}


public class Rebar

{

    public string Number { get; set; }

    public int Type { get; set; }

    public int Fi { get; set; }

}

我有清单:


List<RebarUnique> rebarUnique;


[{{Number: 1, Type: 1, Fi: 12}, (0,0)},


{{Number: 1, Type: 1, Fi: 12}, (0,10)},


{{Number: 2, Type: 1, Fi: 12}, (15,18)},


{{Number: 3, Type: 1, Fi: 12}, (25,34)},


{{Number: 1, Type: 1, Fi: 12}, (25,34)},


{{Number: 3, Type: 1, Fi: 12}, (13,38)}]

我想得到一个清单:


通过...分组 Number


按Point2d( Y coordinate)排序


返回Number具有最低值的列表 ( )Y coordinate


萧十郎
浏览 188回答 2
2回答

莫回无

这样的事情应该工作:var&nbsp;rebarUniqueL&nbsp;=&nbsp;rebarUnique &nbsp;&nbsp;&nbsp;&nbsp;.GroupBy(x&nbsp;=>&nbsp;x.MRebar.Number) &nbsp;&nbsp;&nbsp;&nbsp;.Select(x&nbsp;=>&nbsp;x.OrderBy(y&nbsp;=>&nbsp;y.Point2d.Y).First()) &nbsp;&nbsp;&nbsp;&nbsp;.ToList();

陪伴而非守候

rebarUnique.OrderBy(x => x.Point2d.Y) // Order the items by Y&nbsp; &nbsp; .GroupBy(x => x.MRebar.Number) //Then group them by number, within each group the items are still sorted by Y&nbsp; &nbsp; .Select(g => new {Number = g.Key, Item = g.First()) // get lowest Y for each number&nbsp; &nbsp; .ToList();输出:Number&nbsp; &nbsp;Item&nbsp; &nbsp;&nbsp;1&nbsp; &nbsp; &nbsp; &nbsp; {{Number: 1, Type: 1, Fi: 12}, (0,0)}2&nbsp; &nbsp; &nbsp; &nbsp; {{Number: 2, Type: 1, Fi: 12}, (15,18)}3&nbsp; &nbsp; &nbsp; &nbsp; {{Number: 3, Type: 1, Fi: 12}, (25,34)},
随时随地看视频慕课网APP
我要回答