2012-11-15 27 views
10

Tôi có hai lớp (Yêu cầu & RequestDetail). Tôi cần truy vấn Linq To NHibernate giữa hai lớp bằng cách tham gia.Cách sử dụng kết hợp với nhiều điều kiện trong linq-to-Nhibernate

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(); 

Khi tôi thêm một điều kiện Where để q2, quả có một lỗi runtime. Thông điệp của ngoại lệ là: Specified method is not supported.

Stack Trace:

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) 

Tại sao?

Trả lời

15

Nó có thể là một lỗi.

Dưới đây là cách giải quyết cho vấn đề của bạn:

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(); 
+5

câu trả lời này cần phải được công bố đến thế giới! Đây là cách nó có thể và không hoạt động! Tôi không thể tin được bao nhiêu lần chúng tôi đã xem xét và không tìm thấy bất kỳ giải pháp nào khác ngoài các công cụ QueryOver kinh khủng, hoặc bị buộc phải tạo ra các ánh xạ mới! – user2415376

1

Trong thạo-ký hiệu:

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(); 
Các vấn đề liên quan