I am trying to run an IN statement through Dynamic LINQ by using the Contains method. When using the following code, it runs just fine:
var statuses = new List<string> { "Active" };
result = result.Where("@0.Contains(Status)", statuses.ToArray());
But when I switch it to an array of integers, I get an error:
var years = new List<int> { 2012 };
result = result.Where("@0.Contains(Year)", years.ToArray());
This is the error I receive using the later:
No applicable method ‘Contains’ exists in type ‘Int32’
Is there a way to do this without having to resort to using multiple OR statements to get around using the IN statement?
The fields in the database that these are looking up are nullable fields. Is that was is causing the issue? I switched it from int to int? and it still shows the same error. Any suggestions?
Well, after some research, it seems that Contains doesn’t work “out-of-the box”. I’m really surprised when you say that your first query works. I tried it, and had the same error as with the “Int32” one.
You might find a workaround here :
http://blog.walteralmeida.com/2010/05/advanced-linq-dynamic-linq-library-add-support-for-contains-extension-.html
You need to modify the System.Linq.Dynamic library by yourselft, or download the sample project here :
http://walteralmeida.typepad.com/files/dynamiclinqextension.zip
But the article is maybe outdated now (don’t know about System.Linq.Dynamic’s evolution since 20109.