Split List into Sublists with LINQ

前端 未结 30 2922
灰色年华
灰色年华 2020-11-21 06:26

Is there any way I can separate a List into several separate lists of SomeObject, using the item index as the delimiter of each s

30条回答
  •  孤城傲影
    2020-11-21 07:05

    Check this out! I have a list of elements with a sequence counter and date. For each time the sequence restarts, I want to create a new list.

    Ex. list of messages.

     List messages = new List
            {
                new { FcntUp = 101, CommTimestamp = "2019-01-01 00:00:01" },
                new { FcntUp = 102, CommTimestamp = "2019-01-01 00:00:02" },
                new { FcntUp = 103, CommTimestamp = "2019-01-01 00:00:03" },
    
                //restart of sequence
                new { FcntUp = 1, CommTimestamp = "2019-01-01 00:00:04" },
                new { FcntUp = 2, CommTimestamp = "2019-01-01 00:00:05" },
                new { FcntUp = 3, CommTimestamp = "2019-01-01 00:00:06" },
    
                //restart of sequence
                new { FcntUp = 1, CommTimestamp = "2019-01-01 00:00:07" },
                new { FcntUp = 2, CommTimestamp = "2019-01-01 00:00:08" },
                new { FcntUp = 3, CommTimestamp = "2019-01-01 00:00:09" }
            };
    

    I want to split the list into separate lists as the counter restarts. Here is the code:

    var arraylist = new List>();
    
            List messages = new List
            {
                new { FcntUp = 101, CommTimestamp = "2019-01-01 00:00:01" },
                new { FcntUp = 102, CommTimestamp = "2019-01-01 00:00:02" },
                new { FcntUp = 103, CommTimestamp = "2019-01-01 00:00:03" },
    
                //restart of sequence
                new { FcntUp = 1, CommTimestamp = "2019-01-01 00:00:04" },
                new { FcntUp = 2, CommTimestamp = "2019-01-01 00:00:05" },
                new { FcntUp = 3, CommTimestamp = "2019-01-01 00:00:06" },
    
                //restart of sequence
                new { FcntUp = 1, CommTimestamp = "2019-01-01 00:00:07" },
                new { FcntUp = 2, CommTimestamp = "2019-01-01 00:00:08" },
                new { FcntUp = 3, CommTimestamp = "2019-01-01 00:00:09" }
            };
    
            //group by FcntUp and CommTimestamp
            var query = messages.GroupBy(x => new { x.FcntUp, x.CommTimestamp });
    
            //declare the current item
            dynamic currentItem = null;
    
            //declare the list of ranges
            List range = null;
    
            //loop through the sorted list
            foreach (var item in query)
            {
                //check if start of new range
                if (currentItem == null || item.Key.FcntUp < currentItem.Key.FcntUp)
                {
                    //create a new list if the FcntUp starts on a new range
                    range = new List();
    
                    //add the list to the parent list
                    arraylist.Add(range);
                }
    
                //add the item to the sublist
                range.Add(item);
    
                //set the current item
                currentItem = item;
            }
    

提交回复
热议问题