14

I am facing some problem while executing the query having 'Contains' keyword in Dynamic linq in C#. I am getting the below error

No property or field exists in type 'Int32'

My code is as below:

If I user the 'Contains' keyword for datatype string field, then it works fine as below

string[] CandidateNamesArray = new string[]{"Ram", "Venkat", "Micheal"}
var dynamicLinqQuery = Candidates.Where("CandidateName.Contains(@0)", CandidateNamesArray );
  • works fine

But if I use the 'Contains' keyword for datatype int field, then it throws exception as below

int[] CandidateIdsArray = new int[]{4, 78, 101}
var dynamicLinqQuery = Candidates.Where("CandidateId.Contains(@0)", CandidateIdsArray);

Runtime Exception - "No applicable method 'Contains' exists in type 'Int32'"

Also tried in another way as below

int[] CandidateIdsArray = new int[]{4, 78, 101}
var dynamicLinqQuery = Candidates.Where("@0.Contains(CandidateId)", CandidateIdsArray);

Runtime Exception - "No property or field 'CandidateId' exists in type 'Int32'"

I have spend almost 2 days to resolve the above problem but not able to succeed. Could any one please help me out in resolving the above issue...Thanks in Advance

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
venkat
  • 153
  • 1
  • 2
  • 9
  • 3
    The error very clearly states the problem. `String` has a method called `Contains` which checks if the string contains a sequence of characters which you specify to the method. `Int32` does not have a method called `Contains`, what would such a method do? That is your first error. In the second one, you try the `Contains` method on `int[]`. There is no such method (there is an extension method). Try a `List` see if you have better luck. – odyss-jii Mar 26 '13 at 09:11
  • Looks like your third sample should work. Are you sure that you have `Candidates.Where(...)` and not `CandidateIdsArray.Where(...)` ? – alex Mar 26 '13 at 09:27
  • @venkat, did you try a `List`? – odyss-jii Mar 26 '13 at 09:42
  • I have tried with List, ended with same exception "No property or field 'CandidateId' exists in type 'Int32'". And in the third sample, it is CandidateIdsArray.Where(...) – venkat Mar 26 '13 at 10:03
  • Waait, if in third sample you have `CandidateIdsArray.Where(...)`, then that is the problem! Put `Candidate.Where(...)` there. – alex Mar 26 '13 at 10:51
  • Hey sorry i got confused, it is correct only as below Candidates.Where("@0.Contains(CandidateId)", CandidateIdsArray) here where @0 is replaced with CandidateIdsArray – venkat Mar 26 '13 at 11:10
  • I am trying to use the equivalent of your third query to see if an integer property of object in an observable collection is in a list of "allowed" properties. I have had no luck finding a solution, and this SO question doesn't seem to have an answer. Any luck on this one? – Jon Oct 08 '14 at 17:03
  • @venkat did you ever solve this problem/have the code I'm encountering the same issue – johnny 5 Oct 15 '14 at 19:10

5 Answers5

9

I know it's a long time from your post, but I faced the same issue today.

I solved it using outerIt before the property inside the Contains.

In your example:

int[] CandidateIdsArray = new int[]{4, 78, 101}
var dynamicLinqQuery = Candidates.Where("@0.Contains(outerIt.CandidateId)", CandidateIdsArray);

It worked for me because DynamicLinq was thinking CandidateId was a property of the array's object. And using outerIt made it understand that it refers to the outer iterator, which is the Candidate.

Rick Wolff
  • 769
  • 11
  • 25
1

You can use convert your array to string, then make Contains() than convert it back to int

Alex
  • 8,827
  • 3
  • 42
  • 58
  • Thanks, but in query I am filtering with the DB entity object field 'CandidateId' which is of datatype int which i cannot change. And need to filter this CandidateId with the list of int array values, how can i do that? – venkat Mar 26 '13 at 09:17
  • @venkat Can't you make something like var query = from can in Candidates select new { can.CandidateId.ToString()}; and then add Where() on your query? – Alex Mar 26 '13 at 09:28
  • Let me explain you in detail... My whole code follows as below //1st Linq query IQueryable obj_Query = (from c in vvEntity.Candidates.ToList() join p in vvEntity.view_Candidate_Attributes.ToList() on c.CandidateId equals p.CandidateId join q in view_Geographies.ToList() on c.CandidateId equals q.CandidateId select new { c.CandidateId, c.CandidateName, q.FirstName, q.LastName, c.CreatedOn, c.ModifiedOn }).ToList().AsQueryable(); – venkat Mar 26 '13 at 10:43
  • //2nd dynamic Linq query using the resultset of 1st Linq query. And I opted for dynamic linq query as the parameters in where condition are dynamic. var dynamicLinqQuery = obj_Query.Where("@0.Contains(CandidateId)", CandidateIdsArray); If I want to do normal linq above, tried in below way but the field names are not comming(Intellisense not shows for c.fieldnames in below code) var NormalLinqQuery = obj_Query.Where(c => c.CandidateIdsIntArray.Contains(c.CandidateId)); //syntax error: c.CandidateId Now could you please suggest me how I can proceed? – venkat Mar 26 '13 at 10:44
  • @venkat Well, I suppose dynamic linq has much stuff to work with. You should try to implement your code with IQuerable expresions (http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.expression.aspx), something like Expression> expression = c => return CandidateIdsArray.Contains(c.CandidateId); – Alex Mar 26 '13 at 11:29
1

I dont know Dynamic Linq but it seems obvious to me that type Int32 does not contain any method called Contains. How about converting it to a string before calling Contains ?

var dynamicLinqQuery = Candidates.Where("CandidateId.ToString().Contains(@0)", CandidateIdsArray);
Drewman
  • 947
  • 11
  • 23
  • I have tried as you suggested, ended with below exception "Exception: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression." – venkat Mar 26 '13 at 10:00
0

Looks like you are checking the wrong thing. In first query you actually check whenever field "CandidateName" contains another string, and that works. But in second example you check if field "CandidateId" contains another int. And int cannot contain another int, so you get an error.

Edit:

Looks like in third sample code and exception does not match. You'll get exception "No property or field 'CandidateId' exists in type 'Int32'" if the code was:

int[] CandidateIdsArray = new int[]{4, 78, 101}
var dynamicLinqQuery = 
      CandidateIdsArray.Where("@0.Contains(CandidateId)", CandidateIdsArray);

But the code you provided looks correct and should work:

int[] CandidateIdsArray = new int[]{4, 78, 101}
var dynamicLinqQuery = 
      Candidates.Where("@0.Contains(CandidateId)", CandidateIdsArray);
alex
  • 12,464
  • 3
  • 46
  • 67
  • Thanks, in second example I am checking for candidateIds which are matching with the Ids in int array. How could i do this? suggest if any other way? – venkat Mar 26 '13 at 09:28
  • 1
    Does everything work fine without dynamic linq? with just `int[] CandidateIdsArray = new int[]{4, 78, 101} var dynamicLinqQuery = Candidates.Where(c=>CandidateIdsArray.Contains(c.CandidateId));` – alex Mar 26 '13 at 09:33
  • 1
    Yes, everything works fine with your code without dynamic linq. But I need to use dynamic linq only as the columns I use in where condition are always dynamic. – venkat Mar 26 '13 at 10:13
0

Try the following:

var validCandidateIds = new List<int>(){4, 78, 101};
var filteredCandidates = Candidates.Where(
    "@0.Contains(CandidateId)", validCandidateIds);

Should be equal to (but probably slower than):

var validCandidateIds = new List<int>(){4, 78, 101};
var filteredCandidates = Candidates.Where(
    c => validCandidateIds.Contains(c.CandidateId));
matthias.lukaszek
  • 2,200
  • 1
  • 23
  • 33