0

I have to find string items in FirstName and LastName column will have data with one or more words as shown below. I need to find the matching records which the search text should satisfy at least anyone word in both columns.

i.e : FirstName and LastName. How to search using LINQ query?

In database:

FirstName: Ashok Kumar

LastName: Mahesh Prasad

SearchText = ashok mahesh abc xyz

Here ashok is matching in Firstname and mahesh is matching in lastname. How to search this using below LINQ query?

Sample Linq Query:

getQuery = (_uow.GetRepository<EntityModel.UserOrganization>().GetAll().Where ( x => x.User.FirstName.ToLower().Contains(SearchText.ToLower()) )

Thanks in advance

user1528398
  • 53
  • 1
  • 6

1 Answers1

0

Use function FilterByItems from this answer. And use for your case.

var searchWords = SearchText.ToLower().Split(' ');

getQuery = _uow.GetRepository<EntityModel.UserOrganization>().GetAll()
    .FilterByItems(searchWords, 
        (x, w) => x.User.FirstName.ToLower().Contains(w) 
            || x.User.LastName.ToLower().Contains(w), true);
Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32