Reputation: 97
What I want to do is :
var TheQuery = db.Conventions.Where(p => p.Categories.Contains("%"));
but it nothing is returned from database!
Upvotes: 1
Views: 107
Reputation: 114731
Based on your comment to DaveShaw I think this is what you should do:
IQueryable<Convention> query = db.Conventions;
if (!string.IsNullOrWhiteSpace(inputCategory))
{
query = query.Where(p => p.Categories.Contains(inputCategory));
}
if (!string.InNullOrWhiteSpace(inputName))
{
query = query.Where(p=> p.Name == inputName);
}
etc.
Now if you want to make sure that your Convention has at least one category, you can instead use .Any()
if (string.IsNullOrWhiteSpace(inputCategory))
{
query = query.Where(p => p.Categories.Any());
}
or if it should have any category with a specific name
if (!string.IsNullOrWhiteSpace(inputCategory))
{
query = query.Where(p => p.Categories.Any(c => c.Name.Contains(inputCategory));
}
The actual query isn't executed until you call ToArray, ToList or ToEnumerable on it (or any other method that requires client side evaluation). As long as you stick to pure LINQ calls on the Query object, you can continue to stick where clauses to it until you're satisfied.
Upvotes: 1
Reputation:
The SQL "%"? Do you mean when using with a LIKE? If so just use .Contains("Products")
to bring back Categories such as:
carProducts
houseProducts2
i.e. The contains will bring back results where the Categories property/field "contains" the string, regardless of what is either side of it.
Or did you mean you would like the Categories to contain the character '%'? (You are successfully doing that now)
Upvotes: 0