简体   繁体   中英

Convert this row_number query to Entity Framework Linq to Object query

Original query:

select min(id) as startid, max(id) as endid
from (select t.*, floor((row_number() over (order by id) - 1) / 200) as grp
      from t
      where t.x = y
      ) t
group by grp;

This is a follow up question of: Sql Range Groups Start and End Id

I'm wondering if it is possible to convert this to a Linq to Object query? I've tried looking around for ideas and played with .Skip() .Take() but was unable to get anything close to what I need. Thanks.

Edit: I would like the entire transaction to happen in the database. This will be a fairly large set of data and it would be best if I don't have to process it further in my application.

Try this and you may correct index borders selection condition as you wanted.:

var query = context.YourTable.Where(x => x.t == "your y variable").ToList()
    .OrderBy(x => x.ID).Select((x, index) => new { item = x, index = index })
    .GroupBy(x => x.index / 200 )
    .Select(x => new { minID = x.Min(y => y.item.ID), maxID = x.Max(y => y.item.ID) }).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM