如何在linq-to-Nhibernate中使用多个条件的连接

Ehs*_*san 13 c# linq nhibernate join linq-to-nhibernate

我有两个类(Request&RequestDetail).我需要Linq To NHibernate通过join在两个类之间进行查询.

var q = SessionInstance.Query<Request>()
       .Where(x => x.State == "Init");

var q2 = SessionInstance.Query<RequestDetail>();
q2 = q2.Where(xx => xx.Purpose.Contains("Purpose Sample")); // This line has a error When execution ??`q.ToList()?`

q = q.Join(q2, request => request.Id, detail => detail.Id, (request, detail) => request);

return q.ToList();
Run Code Online (Sandbox Code Playgroud)

当我添加Where条件时q2,Result有一个运行时错误.异常消息是:Specified method is not supported.

堆栈跟踪 :

   at NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.GetClassName(IASTNode querySource)
   at NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.Process(IASTNode tree)
   at NHibernate.Hql.Ast.ANTLR.AstPolymorphicProcessor.Process()
   at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IASTNode ast, String queryIdentifier, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
   at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryIdentifier, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
   at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters)
   at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow)
   at NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression)
   at NHibernate.Linq.NhQueryProvider.PrepareQuery(Expression expression, IQuery& query, NhLinqExpression& nhQuery)
   at NHibernate.Linq.NhQueryProvider.Execute[TResult](Expression expression)
   at Remotion.Data.Linq.QueryableBase`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
Run Code Online (Sandbox Code Playgroud)

为什么?

SHS*_*HSE 22

这可能是一个错误.

以下是您的问题的解决方法:

var q = (from request in session.Query<Request>()
        join detail in session.Query<RequestDetail>() on request.Id equals detail.Id
        where request.State == "Init" && detail.Purpose.Contains("Purpose Sample")
        select request).ToList();
Run Code Online (Sandbox Code Playgroud)

  • 这个答案需要向全世界宣布!这是它能够并且确实有效的方式!我不敢相信我们看了多少次,除了可怕的QueryOver之外没有找到任何解决方案,或者被迫创建新的映射! (6认同)

Phi*_*ker 7

流利的表示法:

var q = session.Query<Request>()
        .Join(session.Query<RequestDetail>(), request => request.Id, detail => detail.Id, (request, detail) => new {
            r = request,
            d = detail
        })
        .Where(rd => rd.r.State == "Init" && rd.d.Purpose.Contains("Purpose Sample"))
        .Select(rd => rd.r)
        .ToList();
Run Code Online (Sandbox Code Playgroud)