:::: MENU ::::

ROW_NUMBER() in LINQ 2 SQL

Last week was quite simple and went without trouble  , and just before I start enjoying my weekends – stuck in to in issue of getting ROW_NUMBER() (as we are doing in sandard SQL Query !

Finally come-up with sort and sweet code!

var items = context.ItemMasters.AsEnumerable()

.Select((u, index) =>  new ItemDetail

{    ItemId = u.ItemId,

ItemName = u.ItemName,

IsActive = (bool)u.IsActive,

Count = index + initialRow + 1

}).Skip(initialRow).Take(finalRow).ToList();

Please post comment if any other better solutions is possible for the same.

Thanks

Happy Coding !

 


4 Comments

  • Reply Bharat Patel |

    Hi Vinay,

    can you check with this statement. it will give rownumber like. 1,2,3,4….

    ROW_NUMBER() over(order by itemid) as rownum

  • Reply July |

    select * from(select ROW_NUMBER() over(order by id) n, * from [Product])t
    where name=’jljljljlj’

    can you make it to linq?

    • Reply Vinay Patel |

      var productList = product.Select((r, i) => new { Row = r, Index = i })
      .Where(x => x.Row.Name== ‘filter value’)

      This would work for you.

So, what do you think ?