Reputation: 16920
I have a textbox in which the user can type keywords to search for by including and
and or
keywords to make the keywords work like a SQL Filter. How can I perform this type of search using Linq? If possible, please also take parentheses (()
) into consideration.
Upvotes: 2
Views: 1256
Reputation: 7777
If it's going to be free text entry, e.g.
LastName = "Smith" and FirstName = "John"
then you may find it helpful to use Scott Guthrie's Dynamic Linq library, which provides a Where()
method that accepts a string that it parses into a lambda. Then you could do something like this:
string searchQuery = GetSearchQueryFromTextBox();
var searchResult = customers.Where(searchQuery);
It supports the and
and or
operators, as shown above, as well as parentheses and host of other operators and methods.
Upvotes: 2
Reputation: 27451
from s in search
where
((s.operatorKeyword == "and" && (s.property1 == "1" && s.property2 == "2")) ||
(s.operatorKeyword == "or" && (s.property1 == "1" || s.property2 == "2")))
select s
Upvotes: 0