我有 3 个类:Person、Employee1、Employee2
public class Employee1 : Person
{
}
public class Employee2 : Person
{
}
我需要对 Person_Table 进行查询,该查询需要一段时间才能加入 Employee1_Table 或 Employee_Table。
var q = SessionInstance.Query<Person>();
if (dto.Code != null) // A1 Condition
q = q.Where(x => x.Code == dto.Code);
//if(A2 Condition)
//if(A3 Condition)
//...
switch (dto.Type)
{
case PersonType.Employee1:
var q1 = SessionInstance.Query<Employee1>();
q.Join(q1, x => x.Id, xx => xx.Id, (x, xx) => x);
if (!String.IsNullOrEmpty(dto.Unit)) // B1 Condition
q1 = q1.Where(xx => xx.Unit == dto.Unit);
//if(B2 Condition)
//if(B3 Condition)
//...
return q1.ToList<Person>();
case PersonType.Employee2:
var q2 = SessionInstance.Query<Employee2>();
q.Join(q2, x => x.Id, xx => xx.Id, (x, xx) => x);
if (!String.IsNullOrEmpty(dto.Serial)) // C1 Condition
q2 = q2.Where(xx => xx.Serial == dto.Serial);
//if(C2 Condition)
//if(C3 Condition)
//...
return q2.ToList<Person>();
default:
return q.ToList();
}
此联接查询不完整。如果dto.Type等于PersonType.Employee1或PersonType.Employee2,A1 , A2 , ...
则不影响。但是对于 switch 的默认情况,A1 , A2 , ...
对查询有影响。我在 3 个单独的类中的 Where 条件很多,我需要Where
单独添加查询条件。为什么?
更新 :
var q = SessionInstance.Query<Person>();
if (dto.Code != null) // A1 Condition
q = q.Where(x => x.Code == dto.Code);
//if(A2 Condition)
//if(A3 Condition)
//...
var q1 = SessionInstance.Query<Employee1>();
if (!String.IsNullOrEmpty(dto.Unit)) // B1 Condition
q1 = q1.Where(xx => xx.Unit == dto.Unit);
//if(B2 Condition)
//if(B3 Condition)
//...
return q.Join(q1, x => x.Id, xx => xx.Id, (x, xx) => x).ToList<Person>();
B1
如果条件为真,则此更新的查询会出现异常。此异常的消息是:不支持指定的方法。
堆栈跟踪是:
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)