C # Lambda.Contains () for multiple properties

We use the following to generate a search query (using NHibernate).

GetAll(x => x.Username.ToUpper().Contains(SEARCH)).ToList(); 

Is it possible to search (contains) for several properties, something like ...

  GetAll(x => x.Username.ToUpper().Contains(SEARCH) && x => x.Firstname.ToUpper().Contains(SEARCH) && x => x.Lastname.ToUpper().Contains(SEARCH)).ToList(); 

Using C #

+6
contains c # lambda search
source share
2 answers
 GetAll(x => x.Username.ToUpper().Contains(SEARCH) && x.Firstname.ToUpper().Contains(SEARCH) && x.Lastname.ToUpper().Contains(SEARCH)).ToList(); 

I would suggest that you want to do an OR search, though:

 GetAll(x => x.Username.ToUpper().Contains(SEARCH) || x.Firstname.ToUpper().Contains(SEARCH) || x.Lastname.ToUpper().Contains(SEARCH)).ToList(); 
+6
source share

Try NinjaNye.SearchExtensions .

It will allow you to use the following syntax:

 var result = GetAll().Search("search", x => x.Username, x => x.Firstname, x => x.Lastname) .ToList(); 

When used with sql, this will result in the following:

 SELECT [Extent1].[Id] AS [Id], [Extent1].[Username] AS [Username], [Extent1].[Firstname] AS [Firstname], [Extent1].[Lastname] AS [Lastname] FROM [dbo].[Users] AS [Extent1] WHERE ([Extent1].[Username] LIKE N'%search%') OR ([Extent1].[Firstname] LIKE N'%search%') OR ([Extent1].[Lastname] LIKE N'%search%') 

... means that all work is done in the data source, not in memory

For the source code, see the github page:

https://github.com/ninjanye/SearchExtensions

0
source share

All Articles