Get item index (rownum)

偶尔善良 提交于 2019-12-12 09:57:10

问题


I have table with 10 000 elements.

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

I need to get the index(rownum) of selected obj without getting all table items

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

The simple sql work fine :

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

How do this in Linq to NHibernate ?

Edit:

I tried add to IEntity class property RowNumber and mapping this on hbm as

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

But after

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

Get always 1 value


回答1:


Can you not just filter the query directly?

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

Edit:

To get the item you can project 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();


来源:https://stackoverflow.com/questions/10226118/get-item-index-rownum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!