如何建立一个IEnumerable&LT; INT&GT;。载有()前pression? [英] How to build an IEnumerable<int>.Contains() Expression?
问题描述
我目前用ASP动态数据的工作,第一次,我试图建立一个过滤器。我们的用户需要找到基于一个列表项的项目是否是一个选择的父母的孩子(我们的项目可以有多个父)。
在有关项目段,每段有一个属性叫做RouteIds,IEnumerable类型,这是所有段的父ID的集合的。
我已经得到了这一点,在我的过滤器重写GetQueryable方法,但我不断收到扔上显示的最后一行例外:
ConstantEx pression CE =前pression.Constant(int.Parse(this.ddlRouteNames.SelectedValue));
ParameterEx pression PE =前pression.Parameter(source.ElementType);
MemberEx pression我=前pression.Property(PE,this.Column.Name);
VAR callEx pression =前pression.Call(typeof运算(可枚举),包含,新类型[] {} me.Type,CE,我);
思想是,用户会从DropDownList中选择合适的路线,然后我会检查,看看是否段的RouteIds属性包含路由的标识。
这是如何得到这个工作的任何指针?
编辑 - 在这里是个例外:
没有泛型方法的类型'包含'System.Linq.Enumerable'是
与提供的类型参数和参数兼容。没有类型
参数应提供如果方法不通用。
块引用>解决方案有在code两个问题:
- 您的参数是倒退。第一个参数必须是收集,第二你正在寻找的项目。
- 您的类型参数是
的IEnumerable&LT; INT方式&gt;
,当它应该只是INT
因此,固定的code是:
VAR callEx pression =前pression.Call(
typeof运算(可枚举),包含,新的[] {typeof运算(INT)},我,CE);但好像你的前pression的所有部分实际上不是动态的,所以也许类似的东西下面将工作太:
防爆pression&LT; Func键&lt;段,布尔&GT;&GT;前pression =
S =&GT; s.RouteIds.Contains(int.Parse(this.ddlRouteNames.SelectedValue));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 the item is a child of a selected parent (our items can have more than one parent).
The items in question are Segments and each Segment has a property called RouteIds, of type IEnumerable, which is a collection of all of the Segment's parent Ids.
I've gotten to this point in overriding the GetQueryable method in my filter, but I keep getting exceptions thrown on the last line shown:
ConstantExpression ce = Expression.Constant(int.Parse(this.ddlRouteNames.SelectedValue)); ParameterExpression pe = Expression.Parameter(source.ElementType); MemberExpression me = Expression.Property(pe, this.Column.Name); var callExpression = Expression.Call(typeof(Enumerable), "Contains", new Type[] { me.Type }, ce, me);
The thought is that a user would select the appropriate Route from a DropDownList and then I'd check to see if the Segment's RouteIds property contains that Route's Id.
Any pointers on how to get this working?
Edit - Here is the exception:
No generic method 'Contains' on type 'System.Linq.Enumerable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.
解决方案There are two problems in your code:
- Your parameters are backwards. The first parameter has to be the collection, the second the item you're searching for.
- Your type argument is
IEnumerable<int>
, when it should be justint
.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));
这篇关于如何建立一个IEnumerable&LT; INT&GT;。载有()前pression?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!