Lambda expressions with multiple search criteria

I am currently browsing the list to find Customers who match the address. I need to match both the address and the city. How to rewrite a lambda expression to match the criteria of both?

CustomerList.FindAll (function (c) c.Address = ToMatch address)

+3
source share
2 answers

The keyword you are looking for is this AndAlso. It combines two separate checks into one, which is evaluated as Trueif both components are alsoTrue

CustomerList.FindAll(Function(c) c.Address = addressToMatch AndAlso c.City = cityToMatch)
+9
source

There is a nuget package that allows you to search for strings by several properties.

https://www.nuget.org/packages/NinjaNye.SearchExtensions/

This will allow the use of the following code ...

CustomerList.Search(addressToMatch, 
                    Function(c) c.Address, 
                    Function(c) c.City)

When connecting to sql database, the created sql will look like:

SELECT [Extent1].[Address] AS [Address], 
       [Extent1].[City] AS [City]
FROM   [dbo].[Table] AS [Extent1]
WHERE ([Extent1].[Address] LIKE N'%searchTerm%') 
   OR ([Extent1].[City] LIKE N'%searchTerm%') 

Hope this helps

0
source

All Articles