Dynamic Linq failing when using Contains against Int Field

倾然丶 夕夏残阳落幕 提交于 2019-12-11 01:50:46

问题


I'm using Entity Frameworks 4.1.0.0 and MySQL.Data.Entity 6.5.4.0 and when I try and generate a dynamic query for a range of integers, I get an error of:

No applicable method 'Contains' exists in type 'Int32'

This seems to work fine when using a similar structure to check against Strings..but I want to expand this to support the other db fields I have in my data.

Code Example:

        int[] ids = new int[] { 1, 3, 4 };

        IQueryable<entityname> list = db.tablename.Where("Id.Contains(@0)", ids);

I have added in the Dynamic.cs to my project and followed along with http://blog.walteralmeida.com/2010/05/advanced-linq-dynamic-linq-library-add-support-for-contains-extension-.html but there has been no difference then using the Dynamic I loaded via Nuget.

Thank you in advance.


回答1:


The syntax is slightly different:

IQueryable<entityname> list = db.tablename.Where("@0.Contains(outerIt.Id)", ids);

following the link you refer to.




回答2:


If you need to check if a given (variable) int value is contained within a entity column, you can do the following using Dynamic Linq:

return query.Where(String.Format("{0}.ToString().Contains(@0)", field), value);

Check out this answer for an extension method that can perform such task with strings, integers and booleans column types in a rather seamless way.



来源:https://stackoverflow.com/questions/13651080/dynamic-linq-failing-when-using-contains-against-int-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!