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

8
  • 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<int> see if you have better luck. Commented Mar 26, 2013 at 9:11
  • Looks like your third sample should work. Are you sure that you have Candidates.Where(...) and not CandidateIdsArray.Where(...) ? Commented Mar 26, 2013 at 9:27
  • @venkat, did you try a List<int>? Commented Mar 26, 2013 at 9:42
  • I have tried with List<int>, ended with same exception "No property or field 'CandidateId' exists in type 'Int32'". And in the third sample, it is CandidateIdsArray.Where(...) Commented Mar 26, 2013 at 10:03
  • Waait, if in third sample you have CandidateIdsArray.Where(...), then that is the problem! Put Candidate.Where(...) there. Commented Mar 26, 2013 at 10:51

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. That's exactly the solution I needed. "outerIt"
"outerIt" does not work in older versions of the library. Make sure you are using newet one
That's exactly the solution I needed. "outerIt" saved my time, thanks
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);

1 Comment

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."
1

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

5 Comments

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 Can't you make something like var query = from can in Candidates select new { can.CandidateId.ToString()}; and then add Where() on your query?
Let me explain you in detail... My whole code follows as below //1st Linq query IQueryable<object> 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();
//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 Well, I suppose dynamic linq has much stuff to work with. You should try to implement your code with IQuerable expresions (msdn.microsoft.com/en-us/library/…), something like Expression<Func<Candidate, bool>> expression = c => return CandidateIdsArray.Contains(c.CandidateId);
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);

3 Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.