0

I have a LINQ statement like this:

var query = from s in persons
            where (string.IsNullOrEmpty(name) || s.Name.trim().contains(name)) &&
                  (string.IsNullOrEmpty(email) || s.Email.contains(email)) &&
                  (string.IsNullOrEmpty(zip) || s.Zip == zip)
            select s;

The name field is coming from the user who can type the name on the web page and then I need to search the persons table for that particular name. Unfortunately, the name First Name and Last name are in the same column in the database and the other issue is there are two spaces between first Name and Last Name so for e.g. if the name is Steve Ramsey. the name in the database is this way:

Steve  Ramsey

Not all the names are stored this way, only few names are like this. I have the Regex to eliminate one extra space:

Regex regex = new Regex("[ ]{2,}", options);
CustName = regex.Replace(CustName, " ");

I am not sure how to put this regex in LINQ query

5
  • regex.Replace(s.Name, " ").Contains(name) Commented Jun 7, 2023 at 23:34
  • you can use split with space : stackoverflow.com/questions/20056557/using-split-in-linq-query Commented Jun 8, 2023 at 0:43
  • Instead of Regex you can split input name into first and last parts, concatenate them using one and two spaces and then search for either concatenations. Commented Jun 8, 2023 at 4:22
  • Which ORM do you use and which version? Commented Jun 8, 2023 at 5:38
  • What LINQ are you using: LINQ to Objects / SQL / EF 4.x / EF 6.x / EF Core 2.0 / 2.1 / 3.x / 5.x / 6.x / 7.x / 8.x? What database provider? Commented Jun 9, 2023 at 18:14

0

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.