我正在尝试这样做:
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-01
,100-02
等等。
字符串数组的值locations
可能类似于:[100, 101]
。
我想找到的任何内容都MyEntity
以locations
.
可选:
有没有我可以做的映射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);
}