Mark
Mark

Reputation: 3123

Dynamic Linq failing when using Contains against Int Field

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.

Upvotes: 2

Views: 2547

Answers (2)

Darkseal
Darkseal

Reputation: 9564

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.

Upvotes: 0

Gert Arnold
Gert Arnold

Reputation: 109252

The syntax is slightly different:

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

following the link you refer to.

Upvotes: 6

Related Questions