2

我需要IQueryable在基于域实体的业务逻辑程序集中。由于大量类似实体,我想为此目的使用自动映射器。

作品:

_repository.GetList<AgentDto>()
        .Select(dto => new Agent{Login = dto.Login, Password = dto.Password})
        .Where(...).ToList();

不起作用(我Where之前无法放置(另一个组件)Select):

_repository.GetList<AgentDto>()
        .Select(dto => ToAgent(dto))
        .Where(...).ToList();

private Agent ToAgent(AgentDto dto)
    {
        return new Agent{Login = dto.Login, Password = dto.Password};
    }

例外:

System.NotSupportedException was caught
  Message=CustomerInfo.Domain.Support.Agent ToAgent(CustomerInfo.DAL.DTO.AgentDto)
  Source=NHibernate
  StackTrace:
       at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitMethodCallExpression(MethodCallExpression expression)
       at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression expression)
       at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitMemberExpression(MemberExpression expression)
       at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression expression)
       at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression)
       at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression expression)
       at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression)
       at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression expression)
       at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.Visit(Expression expression, VisitorParameters parameters)
       at NHibernate.Linq.Visitors.QueryModelVisitor.VisitWhereClause(WhereClause whereClause, QueryModel queryModel, Int32 index)
       at Remotion.Linq.Clauses.WhereClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel, Int32 index)
       at Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection`1 bodyClauses, QueryModel queryModel)
       at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
       at NHibernate.Linq.Visitors.QueryModelVisitor.Visit()
       at NHibernate.Linq.Visitors.QueryModelVisitor.GenerateHqlQuery(QueryModel queryModel, VisitorParameters parameters, Boolean root)
       at NHibernate.Linq.NhLinqExpression.Translate(ISessionFactoryImplementor sessionFactory)
       at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryIdentifier, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
       at NHibernate.Engine.Query.HQLExpressionQueryPlan.CreateTranslators(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
       at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
       at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters, 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.DefaultQueryProvider.PrepareQuery(Expression expression, IQuery& query, NhLinqExpression& nhQuery)
       at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
       at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression)
       at System.Linq.Queryable.Count[TSource](IQueryable`1 source, Expression`1 predicate)
       at CustomerInfo.BLL.Authentication.AgentManagementService.ValidateAgent(String agentLogin, String password) in D:\Projects\CustomerInfo\CustomerInfo.BLL\Authentication\AgentManagementService.cs:line 49
  InnerException: 
4

2 回答 2

5

您需要考虑 NHibernate Linq 提供程序是如何工作的。它无法处理你扔给它的所有东西。NHibernate 的 Linq 提供程序将 lambda 表达式转换为 HQL,并最终转换为 SQL。它只能支持有限的表达式子集。您作为表达式输入的任何内容都必须可转换为 SQL 并在数据库引擎本身中执行。

NHibernate Linq 提供程序是可扩展的。如果您需要使用一些 NH Linq 提供程序不支持的表达式,并且您认为它们可以用 SQL 表示,您可以编写自己的扩展

但是,您的情况非常简单。你不需要扩展 NHibernate 来做到这一点。NHibernate Linq 提供程序不支持您的表达式,但 Linq to objects 支持。只需颠倒查询中的顺序,它应该可以按预期工作:

_repository.GetList<AgentDto>()
    .Where(...).ToList()
    .Select(dto => ToAgent(dto)).ToList();

.ToList()after.Where()将执行 NHibernate 查询并返回AgentDto对象列表。.Select之后的方法实际上是作为 Linq to objects 执行的 -AgentDto在内存中的对象列表上。

于 2012-08-22T16:44:29.843 回答
0

我认为,如果您只是将 Where 放在 Select 之前,就可以了。

于 2020-12-20T13:25:15.370 回答