Query data using "Contains" keyword in Dynamic Linq in C#

c#, dynamic, linq

Solution

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.

Problem

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

Original source