Get position index (rownum)

I have a table with 10,000 items.

 IQuerable<IEntity> query = dataRep.Get<IEntity>()
                                   .Query();

I need to get the index (rownum) of the selected one objwithout getting all the table elements

 var obj = query.Where( x => x.Name == "testName")
                .FirstOrDefault();

Simple sql execution:

 select name, id, r from 
 (
     select name, id, rownum r from collections order by id
 ) where name = 'testName';

How to do it in Linq for NHibernate?

Edit:

I tried adding IEntityclass to the property RowNumberand mapping it to hbmhow

  <property name="RowNumber" formula="rownum" />

But after

  var index = query.Where( x => x.Name == "testName")
                   .Select( x => x.RowNumber)
                   .FirstOrDefault();

Always get 1value

+2
source share
1 answer

Can't you just filter the query directly?

IQuerable<IEntity> query = dataRep.Get<IEntity>()
                                  .Query()
                                  .FirstOrDefault(x => x.Name == "testName");

Edit:

To get an element, you can design it into an anonymous type:

var query = (from data in dataRep.Get<IEntity>().Query()
             where Name == "testName"
             select new
             {
                 id = data.id,
                 rowNumber = data.rowNumber
             }).FirstOrDefault();
+1
source

All Articles