How to separate all consecutive Objects using Linq (C#)

后端 未结 6 625
长发绾君心
长发绾君心 2021-01-14 10:37

I have a List ... MyObject has a propert called LineId (integer)...

So, I need separate all consecutive LineId in List

6条回答
  •  一生所求
    2021-01-14 11:37

    void Main()
    {
        var list = new []{
            new { LineId = 1 },
            new { LineId = 1 },
            new { LineId = 1 },
            new { LineId = 2 },
            new { LineId = 2 },
            new { LineId = 1 },
            new { LineId = 1 },
            new { LineId = 3 },
            new { LineId = 3 },
            new { LineId = 1 }
        };
    
        var groups =    
            from i in list
            group i by i.LineId into g
            select new { LineId = g.Key, MyObject = g };
    }
    

提交回复
热议问题