Using LIKE operator in LINQ to Entity
Posted
by Draconic
on Stack Overflow
See other posts from Stack Overflow
or by Draconic
Published on 2010-06-15T21:00:30Z
Indexed on
2010/06/15
21:02 UTC
Read the original article
Hit count: 744
Hi, everybody!
Currently in our project we are using Entity Framework and LINQ. We want to create a search feature where the Client fills different filters but he isn't forced to.
To do this "dynamic" query in LINQ, we thought about using the Like operator, searching either for the field, or "%" to get everything if the user didn't fill that field.
The joke's on us when we discovered it didn't support Like. After some searching, we read several answers where it's sugested to use StartsWith, but it's useless for us.
Is the only solution using something like:
ObjectQuery<Contact> contacts = db.Contacts;
if (pattern != "")
{
contacts = contacts.Where(“it.Name LIKE @pattern”);
contacts.Parameters.Add(new ObjectParameter(“pattern”, pattern);
}
However, we'd like to stick with linq only.
Happy coding!
© Stack Overflow or respective owner