System.Linq.Dynamic and Sql IN statement

Possible duplicate:
'Contains ()' workaround using Linq for Entities?

I use the System.Linq.Dynamic library to control the basic GUI query engine, however I cannot find support for the equivalent of the Sql IN operator. Has anyone used this?

I searched and found several possible duplicates, however none of them was what I requested (or was resolved).

Clarification:

I use the dynamic query API to create queries in Linq, for example:

var customers = dbDataContext
                 .Clients
                 .Where("Lastname = @0", "Smith");

Which works great, one statement I'm struggling with is equivalent to the Sql IN operator. I would like to do this:

var customers = dbDataContext
                 .Clients
                 .Where("ProductIDsPurchased IN (1,6,8,90)");

However, I'm not sure how to write this using dynamic queries (above does not work).

+3
1

this Stackoverflow ...

:

WhereIn:

public static IQueryable<TEntity> WhereIn<TEntity, TValue>
 (
     this ObjectQuery<TEntity> query, 
     Expression<Func<TEntity, TValue>> selector, 
     IEnumerable<TValue> collection
 )
{
    if (selector == null) throw new ArgumentNullException("selector");
    if (collection == null) throw new ArgumentNullException("collection");
    ParameterExpression p = selector.Parameters.Single();

    if (!collection.Any()) return query;

    IEnumerable<Expression> equals = collection.Select(value => 
      (Expression)Expression.Equal(selector.Body, 
       Expression.Constant(value, typeof(TValue))));

    Expression body = equals.Aggregate((accumulate, equal) => 
    Expression.Or(accumulate, equal));

    return query.Where(Expression.Lambda<Func<TEntity, bool>>(body, p));
}

:

public static void Main(string[] args)
{
    using (Context context = new Context())
    {
        //args contains arg.Arg
        var arguments = context.Arguments.WhereIn(arg => arg.Arg, args);   
    }
}

:

var customers = dbDataContext
             .Clients
             .WhereIn(c=> c.LastName, new List<string>{"Smith","Wesson"});

+6

Source: https://habr.com/ru/post/1760281/


All Articles