1

I have three properties

First name
Last name
Username

User can search for a word that can be in all three properties. I have had this two queries to achieve the desired result

searchTermFilters = searchTermFilters
.For(term)
.InFields(x => x.Firstname, x => x.Surname, x => x.Username);


searchTermFilters = searchTermFilters
            .OrFilter(x => x.Firstname.AnyWordBeginsWith(term))
            .OrFilter(x => x.Surname.AnyWordBeginsWith(term))
            .OrFilter(x => x.Username.AnyWordBeginsWith(term));

The issue is that for both of them it matches exactly. I want something like .Contains in Linq. Any insights?

Ted Nyberg
  • 7,001
  • 7
  • 41
  • 72
mohsinali1317
  • 4,255
  • 9
  • 46
  • 85

2 Answers2

1

Use this way with Or condition

searchTermFilters.Filter(x => x.Firstname.Contains(term) | 
                         x => x.Lastname.Contains(term) |
                         x => x.Username.Contains(term))
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
  • This works unless OP is about finding strings that strictly either _begins_ or _exactly matches_ the string. If it's OK for the string to appear anywhere, this should be the accepted answer. – Ted Nyberg Jul 06 '18 at 08:22
-1

For single Filter=>

searchTermFilters = searchTermFilters.Where(w=>w.FirstName.Contains(term)).ToList();

For Multi Filter=>

searchTermFilters = searchTermFilters.Where(w=>w.FirstName.Contains(term) || w.Surname.Contains(term)).ToList();

Hope this help.

lwin
  • 3,784
  • 6
  • 25
  • 46