How to build an IEnumerable.Contains() Expression?

前端 未结 1 1857
一整个雨季
一整个雨季 2021-01-14 23:26

I\'m currently working with ASP Dynamic Data for the first time and I\'m trying to build a filter. Our users have a need to locate items in a list based upon whether or not

相关标签:
1条回答
  • 2021-01-14 23:35

    There are two problems in your code:

    1. Your parameters are backwards. The first parameter has to be the collection, the second the item you're searching for.
    2. Your type argument is IEnumerable<int>, when it should be just int.

    So, the fixed code is:

    var callExpression = Expression.Call(
        typeof(Enumerable), "Contains", new[] { typeof(int) }, me, ce);
    

    But it seems all the parts of your expression are not actually dynamic, so maybe something like that following would work too:

    Expression<Func<Segment, bool>> expression =
        s => s.RouteIds.Contains(int.Parse(this.ddlRouteNames.SelectedValue));
    
    0 讨论(0)
提交回复
热议问题