简体   繁体   English

NHibernate.Linq和MultiCriteria

[英]NHibernate.Linq and MultiCriteria

Anybody know of a way to batch NHibernate queries using NHibernate.Linq like you can do with MultiCriteria and ICriteria objects? 有人知道使用NHibernate.Linq批量NHibernate查询的方法,就像你可以使用MultiCriteria和ICriteria对象一样吗?

With MultiCriteria I can create something like this: 使用MultiCriteria,我可以创建这样的东西:

var crit = session.CreateMultiCriteria()
                  .Add(session.CreateCriteria(typeof(Entity1)).Add(Restrictions.Eq("Property1","Value"))
                  .Add(session.CreateCriteria(typeof(Entity2)).Add(Restrictions.Eq("Property2","Value2"));               

var result = crit.List();
var list1 = (IList)result[0];
var list2 = (IList)result[1];

It would be nice if I replace the CreateCriteria calls with Linq calls and get something like this: 如果我用Linq调用替换CreateCriteria调用并获得如下内容将会很好:

var crit = session.CreateMultiCriteria()
                .Add(session.Linq<Entity1>().Where(x => x.Property1 == "Value1")
                .Add(session.Linq<Entity2>().Where(x => x.Property2 == "Value2");

var result = crit.List();
var list1 = (IList<Entity1>)result[0];
var list2 = (IList<Entity2>)result[1];

We're using the Linq API for most of our other queries and it would be nice to use the same Linq syntax when we need to run MultiCriteria queries as well. 我们在大多数其他查询中使用Linq API,当我们需要运行MultiCriteria查询时,使用相同的Linq语法会很好。

Thanks. 谢谢。

var query = from q in session.Linq<Person>()
            where q.FirstName.StartsWith(firstName)
            && q.LastName.StartsWith(lastName)
            && q.Phones.Any(p => p.Number.Contains(phone))
            select q;

// This block of code was found in the NHibernate.Linq source
// using NHibernate.Linq.Visitors;
// using NHibernate.Engine;
System.Linq.Expressions.Expression expression = query.Expression;
expression = Evaluator.PartialEval(expression);
expression = new BinaryBooleanReducer().Visit(expression);
expression = new AssociationVisitor((ISessionFactoryImplementor)session.SessionFactory).Visit(expression);
expression = new InheritanceVisitor().Visit(expression);
expression = CollectionAliasVisitor.AssignCollectionAccessAliases(expression);
expression = new PropertyToMethodVisitor().Visit(expression);
expression = new BinaryExpressionOrderer().Visit(expression);
NHibernateQueryTranslator translator = new NHibernateQueryTranslator(session);
object results = translator.Translate(expression, ((INHibernateQueryable)query).QueryOptions);

// My LINQ query converted to ICriteria
ICriteria resultsCriteria = results as ICriteria;
// Convert to criteria that returns the row count
ICriteria rowCountCriteria = CriteriaTransformer.TransformToRowCount(resultsCriteria);

IList multiResults = session.CreateMultiCriteria()
    .Add(resultsCriteria.SetMaxResults(20))
    .Add(rowCountCriteria)
    .List();

IList people = (IList)multiResults[0];
int resultsCount = (int)((IList)multiResults[1])[0];

from http://rndnext.blogspot.com/2009/05/using-nhibernate-multicriteria-and-linq.html 来自http://rndnext.blogspot.com/2009/05/using-nhibernate-multicriteria-and-linq.html

NHibernate.Linq itself uses NHibernateQueryTranslator to translate from the LINQ expression to an ICriteria. NHibernate.Linq本身使用NHibernateQueryTranslator将LINQ表达式转换为ICriteria。 You could do this too, then pass the resulting ICriteria into your IMultiCriteria. 您也可以这样做,然后将生成的ICriteria传递给您的IMultiCriteria。

I have a solution that enables both batching a fetching strategies using NHibernate Linq, but the code is king of complex. 我有一个解决方案,可以使用NHibernate Linq批处理提取策略,但代码是复杂的王者。 It's too much to list here. 在这里列出太多了。 I am going to be talking about it on my blog on devlicio.us pretty soon. 我很快就会在devlicio.us的博客上谈论它。 I'll update this comment when I write the first post. 我写第一篇文章时会更新此评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM