Selecting Consecutive Entries with LINQ to Entities

前端 未结 4 2004
野性不改
野性不改 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:01

    The following code will find every "root".

        var query = this.commercialRepository.GetQuery();
        var count = 2;
        for (int i = 0; i < count; i++)
        {
            query = query.Join(query, outer => outer.Index + 1, inner => inner.Index, (outer, inner) => outer);
        }
    
        var dummy = query.ToList();
    

    It will only find the first item in each group so you will either have to modify the query to remeber the other ones or you could make a query based on the fact that you have the roots and from those you know which indexes to get. I'm sorry I couldn't wrap it up before I had to go but maybe it helps a bit.

    PS. if count is 2 as in this case it means if finds groups of 3.

提交回复
热议问题