In or Contains query using System.Linq.Dynamic

c# entity-framework-6 linq

Accepted Answer

due to yourMyUser.CustomerId If a property may be null, you should provide a nullable int array.customerIds this instance. For instance:

public static IQueryable<T> ApplyCustomerFilter<T>(
    this IQueryable<T> collection, 
    int?[] customerIds) { // < - note here
        return collection.Where("@0.Contains(outerIt.CustomerId)", customerIds);
} 

or change the given array to a nullable ints array:

public static IQueryable<T> ApplyCustomerFilter<T>(
    this IQueryable<T> collection, 
    int[] customerIds) {
        return collection.Where("@0.Contains(outerIt.CustomerId)",
             customerIds.Cast<int?>()); // <- note here
}

Ivan Stoev's suggestions for alternatives in the comments (with them)customerIds array may be standard.int[] array (which need not be nullable arrays):

"@0.Contains(outerIt.CustomerId.Value)" 

This one will also function in both scenarios (whetherCustomerId is nullable or not):

"@0.Contains(Int32(outerIt.CustomerId))"
5
11/27/2017 9:36:14 AM


Related Questions





Related

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