2

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.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Mark
  • 3,123
  • 4
  • 20
  • 31
  • Don't you mean `@0.Contains(Id)`? – alexn Nov 30 '12 at 18:42
  • Normally it would be something like that, but I'm just putting up some test code and specifying the column manually to produce/test out the error. – Mark Nov 30 '12 at 18:44
  • It is the @0.Contains(outerIt.Id). I've implemented the same from that blog. Only issue is that, while nuget will get updated, you'll have to download the source from git and reapply your changes each time there's an update – reckface Sep 04 '13 at 15:54

2 Answers2

6

The syntax is slightly different:

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

following the link you refer to.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
0

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.

Darkseal
  • 9,205
  • 8
  • 78
  • 111