NHibernate Query<T> with detached criteria…

旧街凉风 提交于 2019-12-12 12:56:31

问题


I have that :

var query = session.Query<MyClass>();    

// Here I need to execute a detached criteria, like that :
//  query.UnderlyingCriteria.Add(SpatialExpression.Within("Geo", extent));

var t = query.Select(item => new MyClassView
                                      {
                                          Name, Year, Code
                                      }

Is that a way to do that with Query ? Or maybe another way? I need a IQueryable result...

Thanks


回答1:


the linq provider does not use Criteria under the covers, it uses the AST from the HQL parser. If you really need an IQueryable then you could formulate a query like this

var ids = session.QueryOver<MyClass>()
    .UnderlyingCriteria.Add(SpatialExpression.Within("Geo", extent))
    .Select(myclass => myclass.Id)
    .List<int>();

var query = session.Query<MyClass>()
    .Where(x => ids.Contains(x.Id))
    .Select(item => new MyClassView
    {
        Name, Year, Code
    });

Note: this uses 2 roundtrips however



来源:https://stackoverflow.com/questions/9584950/nhibernate-queryt-with-detached-criteria

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