开发者

Converting this method from IList to IQueryable

Is it possible to convert:

public IList Get() { return Session.CreateCriteria(typeof(T)).List();开发者_运维问答 }

to return IQueryable?

What is the difference between IList and IQueryable?


One of the most important aspects when thinking of SQL and Linq is that returning IList means that the query has been executed. Returning IQueryable keeps open the option of deferring the sql execution later (so you could potentially build on the query outside of this method).

However, this would only be the case with NHibernate when using the Linq to NHibernate provider. The Criteria API is not Linqified for the SQL statements. So realistically in this instance returning IList or IQueryable has no significant difference.


What is possible is to return an IEnumerable like this:

public IEnumerable<T> Get() 
{ 
    return Session.CreateCriteria(typeof(T)).Future<T>(); 
}

This way you can do deferred execution as you do with Linq to SQL.

For more info about the Future method see:

http://ayende.com/Blog/archive/2009/04/27/nhibernate-futures.aspx

You can also simulate the IQueryable by returning the ICriteria interface instead of an IList:

public ICriteria<T> Get() 
{ 
    return Session.CreateCriteria(typeof(T)); 
}

This way you can start building the query outside of your method and finally execute it whenever you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜