venkat
venkat

Reputation: 153

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

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 );

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

Upvotes: 14

Views: 10602

Answers (5)

matthias.lukaszek
matthias.lukaszek

Reputation: 2220

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));

Upvotes: 0

Rick Wolff
Rick Wolff

Reputation: 777

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.

Upvotes: 9

alex
alex

Reputation: 12654

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);

Upvotes: 0

Alex
Alex

Reputation: 8937

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

Upvotes: 1

Drewman
Drewman

Reputation: 947

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);

Upvotes: 1

Related Questions