NHibernate.Linq and MultiCriteria

前端 未结 3 1611
花落未央
花落未央 2021-02-08 00:46

Anybody know of a way to batch NHibernate queries using NHibernate.Linq like you can do with MultiCriteria and ICriteria objects?

With MultiCriteria I can create somethi

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-08 01:11

    var query = from q in session.Linq()
                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

提交回复
热议问题