0

I have a code segment like the following:

var results = business.Where(b => b.Description.ToLower().Contains(request.ServiceName.ToLower()));
var bb = results.Where(r => !r.Zip.Contains('-'));

When I ran this I get an error message in the title. My queries return a complex type that I have defined, and one of the properties' of that complex type is:

public string Zip { get; set; }

I want to exclude all the entries that does not include dash in the zip part.

Keith
  • 5,311
  • 3
  • 34
  • 50
tett
  • 595
  • 3
  • 13
  • 34
  • I'm running this without any problem. Maybe the problem is related to the Zip information. – MJVC Jan 06 '15 at 22:50

2 Answers2

3

It should be double quote ...Contains("-").

var bb = results.Where(r => !r.Zip.Contains("-"));

In addition, you can combine two where clauses into one if you only care about final result.

var results = business.Where(b => 
   b.Description.ToLower().Contains(request.ServiceName.ToLower()) && 
   !b.Zip.Contains("-"));

FYI: If you use Entity Framework, you do not need ToLower().

Win
  • 61,100
  • 13
  • 102
  • 181
2

Assuming this is an Entity Framework query, Char is not a primitive type, hence the error. Try var bb = results.Where(r => !r.Zip.Contains("-"));

Found a similar (duplicate) here: Why does this LinQ query not like chars?

Community
  • 1
  • 1
Keith
  • 5,311
  • 3
  • 34
  • 50