0

我正在尝试这样做:

    public static IQueryable<MyEntity> WhereLocations(
        this IQueryable<MyEntity> query,
        string[] locations)
    {
        return query.Where(x => locations.Any(t => x.Location.StartsWith(t)));
    }

但是,当我这样做时,它给了我一种不受支持的方法;

[NotSupportedException: Specified method is not supported.]
   NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.GetClassName(IASTNode querySource) +71
   NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.Process(IASTNode tree) +136
   NHibernate.Hql.Ast.ANTLR.AstPolymorphicProcessor.Process() +40
   NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IASTNode ast, String queryIdentifier, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory) +89
   NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryIdentifier, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory) +42
   NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters) +234
   NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow) +307
   NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression) +294
   NHibernate.Linq.DefaultQueryProvider.PrepareQuery(Expression expression, IQuery& query, NhLinqExpression& nhQuery) +70
   NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression) +32
   NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression) +37
   Remotion.Linq.QueryableBase`1.GetEnumerator() +53
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +369
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +58

字符串的值MyEntity.Location可能是:100-01100-02等等。

字符串数组的值locations可能类似于:[100, 101]

我想找到的任何内容都MyEntitylocations.

可选

有没有我可以做的映射fluent-nhibernate,我可以根据破折号之前的第一个值和破折号后面的内容将此数据库列映射到两个属性

感谢任何提示或帮助。

解决方案

这是我最终做的事情:

    public static IQueryable<MyEntity> WhereLocations(
        this IQueryable<MyEntity> query,
        string[] locations)
    {
        if (locations.Length == 0)
        {
            return query;
        }

        if (locations.Length == 1)
        {
            return query.Where(x => x.Location.StartsWith(locations[0]));
        }

        var predicate = PredicateBuilder.False<MyEntity>();
        predicate = locations.Aggregate(
            predicate, (current, temp) => current.Or(x => x.Location.StartsWith(temp)));
        return query.Where(predicate);
    }
4

1 回答 1

4

您需要的 SQL 类似于

   x.Location LIKE 'tLoc1' + '%'
OR x.Location LIKE 'tLoc2' + '%'
OR x.Location LIKE 'tLoc3' + '%'
etc.

如果您无法使用Any(),请使用PredicateBuilder在 LINQ 中构造动态 OR 子句。

关于映射,我不知道任何可以采用两个属性并将它们存储为一列的映射构造。您可以在模型中使用额外的未映射属性来执行所需的拆分/合并,但这无助于您查询它们。

于 2013-10-24T18:53:23.803 回答