vendredi 20 décembre 2019

Problem to get method "Contains" when Create Dynamic LambdaExression With IgonreCase

I want to create dynamic Lambda Expression for Method Call Contains for list of string values, the below code work's fine but not ignored string Case Sensitive

ParameterExpression parameter = Expression.Parameter(typeof(E), "x");
IQueryable<E> itemsToFilter = null; //this parameter set from input parameters
parameterName = "Name"; //this parameter set from input parameters
var prop = typeof(E).GetProperty(parameterName, BindingFlags.SetProperty | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
MemberExpression propertyExp=Expression.Property(param, parameterName);
var genericListType = typeof(List<>).MakeGenericType(typeof(string));
IList tmplist = (IList)Activator.CreateInstance(genericListType);
foreach (var obj in orcond.Values)
{
   tmplist.Add(obj);
}
methodin = tmplist.GetType().GetMethod("Contains");
var list = Expression.Constant(tmplist);
var containsMethodExpin = Expression.Call(list, methodin, propertyExp);
comparison = Expression.Lambda<Func<E, bool>>(containsMethodExpin, parameter)
itemsToFilter.Where(comparison);

Note that the above code worked only for entity framework IQueryable but not worked for C# List

then I want to compare string in list with IgnoreCase

I want to call Contains with StringComparer.OrdinalIgnoreCase but when I use

methodin = typeof(List<string>).GetMethod("Contains", new Type[] { typeof(string), typeof(IEqualityComparer<string>) });

'methodin' returns null Please help how to get Contains with IEqualityComparer with correct reflection use.





Aucun commentaire:

Enregistrer un commentaire