0

I am using ASP .Net MVC, and I would like to filter my DB result set, until now I've been using this method:

var auctions = from o in db.auctions select o;

And I'd check if user passed some pattern (searchName) from client form so result set would be filtered:

 if (!String.IsNullOrEmpty(searchName))
 {
 auctions = auctions.Where(a => a.product_name.Contains(searchName));
 }

That's fine if I want to check if product name contains single string variable, but I would need to check if product name contains ANY of array's strings passed as string from client's form and split with blank char something like this:

string[] words = searchName.Split(' ');
auctions = auctions.Where(a => a.product_name.Contains(anyStringInArrayWords));

How could I end up with this anyStringInArrayWords lambda impression sent to auctions.Where()?

3 Answers 3

3

You can rewrite the where condition statement bit differently.

auctions = auctions.Where(a => words.Contains(a.product_name));
Sign up to request clarification or add additional context in comments.

1 Comment

This answer is correct, but here's example I'm not sure if I set correct question, example my auction name is: Test Auction Case, and i want when i type in searchName: Test Case to get this auction after filter.
0

You may use .Any extension method to obtain your result.

var q= auctions.Any(item => words.Any( word=> item.ProductName.Contains(word))

Written by mobile and not tested

Comments

0

I agree with Hakunamatata, in case if you are looking for case insensitive search Contains is overloaded to accepts IEqualityComparer. So you could perform case insensitive search using...

auctions = auctions.Where(a => words.Contains(a.product_name, StringComparer.CurrentCultureIgnoreCase));

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.