Selecting Consecutive Entries with LINQ to Entities

前端 未结 4 1880
野性不改
野性不改 2021-01-12 20:34

I have a database table with rows that each contain a sequential index. I want to select groups of rows that are consecutive based upon this index column. For example, if I

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-12 21:10

    I think this might work pretty efficiently (C# though):

    int[] query = { 1, 3, 4, 5, 7, 9, 10, 11, 12, 15, 16 };
    int count = 3;
    List> numbers = query
       .Where(p => query.Where(q => q >= p && q < p + count).Count() == count)
       .Select(p => Enumerable.Range(p, count).ToList())
       .ToList();
    

提交回复
热议问题