Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

dynamic

linq

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

like image 386
venkat Avatar asked Mar 26 '13 09:03

venkat


People also ask

Can you use LINQ on dynamic?

The Dynamic source file includes a helper library that allows you to express LINQ queries using extension methods that take string arguments instead of type safe operators. To use the Dynamic Expression API, you could simply copy/paste the Dynamic source file in your project.

What is contains in LINQ C#?

LINQ Contains is quantifier operator. In LINQ Contains it also checks with the data sources, to check whether the collection of lists contains their desired element or not, and then it returns the result as either true or false based on the expected outcomes of the result.

What is the data type for LINQ query?

However, the basic rule is very simple: a LINQ data source is any object that supports the generic IEnumerable<T> interface, or an interface that inherits from it. Types such as ArrayList that support the non-generic IEnumerable interface can also be used as a LINQ data source.


3 Answers

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.

like image 130
Rick Wolff Avatar answered Oct 23 '22 05:10

Rick Wolff


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);
like image 21
Drewman Avatar answered Oct 23 '22 07:10

Drewman


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

like image 1
Alex Avatar answered Oct 23 '22 06:10

Alex