c# - Create a predicate builder for (x => listOfInts.Contains(x.ListOfIntsToCheck)) -
c# - Create a predicate builder for (x => listOfInts.Contains(x.ListOfIntsToCheck)) -
i'm trtying build predicate builder homecoming predicate checks whether list of ints contains list of ints. far have this
public static expression<func<t, bool>> dynamicintcontains<t, tproperty>(string property, ienumerable<tproperty> items) { var pe = expression.parameter(typeof(t)); var me = expression.property(pe, property); var ce = expression.constant(items); var phone call = expression.call(typeof(list<int>), typeof(list<int>).getmethod("contains").name, new[] { typeof(int) }, ce, me); homecoming expression.lambda<func<t, bool>>(call, pe); }
t search object contains list of ids 1 of it's properties. tproperty list of ints , property name of list on property.the error getting
additional information: no method 'contains' exists on type 'system.collections.generic.list`1[system.int32]'.
is because calling static method? or trying access method on typeof(list) incorrectly? thanks.
here solution;
public static expression<func<t, bool>> dynamicintcontains<t, tproperty>(string property, ienumerable<tproperty> items, object source, propertyinfo propertyinfo) { var pe = expression.parameter(typeof(t)); var me = expression.property(pe, property.singularise()); var ce = expression.constant(propertyinfo.getvalue(source, null), typeof(list<int>)); var convertexpression = expression.convert(me, typeof(int)); var phone call = expression.call(ce, "contains", new type[] { }, convertexpression); homecoming expression.lambda<func<t, bool>>(call, pe); }
this works off assumption list name plural of it's members. in expression.call passing typeof(list), right method pass in type[]. seems requirement convert memberexpression constant of particular type. thanks.
c# linq reflection predicates
Comments
Post a Comment