Domanda

I'm trying to do this:

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

How ever when I do that, it gives me a method not supported;

[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

The value of string MyEntity.Location could be like: 100-01, 100-02 and so on.

The value of array of string locations could be like: [100, 101].

What I would like to find any of MyEntity that starts with any of the values in locations.

Optionally:

Is there a mapping I could do in fluent-nhibernate that I could map this database column into two properties based on the first value before dash and what ever behind the dash

Appreciate any tips or help on this.

Solution

Here is what I ended up doing:

    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);
    }
È stato utile?

Soluzione

The SQL you need would be something like

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

If you cannot get it to work with Any(), use PredicateBuilder to construct the dynamic OR-clauses in LINQ.

Regarding the mapping, I'm not aware of any mapping construct that can take two properties and store them as one column. You can use extra unmapped properties in your model that performs the required split/concat, but that doesn't help you with querying them.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top