Dynamic Linq Where IN

c# dynamic-linq linq


How would I describe awhere in the criterion forDynamic Linq ?

The fix I tried below must not be effective since it is illogical.

context.Records.Where("@0.Contains(ID)", new object[]{
   new string[] {"1", "2", "3"}
}); // throws error No property or field 'ID' exists in type 'String'

Edit 1:

Thank you everybody, all you have to do is utilizeit or outerIt phrase in yourDynamic.Linq hence, in my case, all I need to do is utilizeouterIt :

context.Records.Where("@0.Contains(outerIt.ID)", new object[]{
   new string[] {"1", "2", "3"}

Here is where you may get additional information and examples: Dynamic Linq query library, advanced Linq: Expand the 'Contains' extension's support

4/2/2018 7:00:07 AM

Accepted Answer

Interesting solutions were shown by others here, however if we utilize them, they become ineffective.Dynamic.Linq Andrew Nahirniakz-12-zzz library. A blog entry on sophisticated Linq searches is also available.Dynamic.Linq which 19-zzz reviews.

as of version1.0.4 The library furthermore offersContains extension is included. It might be carried out as follows:

 query = Contact.GetContactsList()
                .Where("@0.Contains(outerIt.Country)", new List<String>() { "Austria", "Poland" }); 


query = Contact.GetContactsList()
               .Where("@0.Contains(outerIt.Country) && it.BirthDate.Year > @1", new List<string>() { "Austria", "Poland" }, 1955);

The two keywords areit and outerIt It represents the list you gave as a parameter to the program andouterIt reflect the actual collection.

So here is my practical illustration:

context.Records.Where("@0.Contains(outerIt.ID)", new object[]{
   new string[] {"1", "2", "3"}
4/2/2018 6:58:14 AM

Popular Answer

Your own extension method may be written.In the IEnumerable is seen below:

public static class ExtLinq
    public static IEnumerable<TSource> In<TSource, TMember>(this IEnumerable<TSource> source,
            Func<TSource, TMember> identifier, params TMember[] values) =>
         source.Where(m => values.Contains(identifier(m)));

Use it as follows:

context.Records.In(x => x.ID, 1, 2, 3)

If you want theIN Use this IQueryable version, which was developed using the expressions API, to be run on the server to reduce data travel:

public static IQueryable<TSource> In<TSource, TMember>(this IQueryable<TSource> source,
   Expression<Func<TSource, TMember>> identifier, params TMember[] values)
            var parameter = Expression.Parameter(typeof(TSource), "m");
            var inExpression = GetExpression(parameter, identifier, values);
            var theExpression = Expression.Lambda<Func<TSource, bool>>(inExpression, parameter);
            return source.Where(theExpression);
static Expression GetExpression<TSource, TMember>(ParameterExpression parameter, Expression<Func<TSource, TMember>> identifier, IEnumerable<TMember> values)
            var memberName = (identifier.Body as MemberExpression).Member.Name;
            var member = Expression.Property(parameter, memberName);
            var valuesConstant = Expression.Constant(values.ToList());
            MethodInfo method = typeof(List<TMember>).GetMethod("Contains");
            Expression call = Expression.Call(valuesConstant, method, member);
            return call;

The server will run this query (or one similar to it depending on your table) and deliver the required results:

    [Extent1].[ID] AS [ID],
    [Extent1].[Name] AS [Name]
    FROM [dbo].[Records] AS [Extent1]
    WHERE [Extent1].[ID] IN (1, 2, 3)

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow