Princess
Princess

Reputation: 443

Lambda Expressions with multiple search criteria

Currently I am searching through my list to find Customers that match on Address. I need to match on both address and city. How do I rewrite my lambda Expression to match both criteria?

CustomerList.FindAll(Function(c) c.Address = addressToMatch)

Upvotes: 3

Views: 2994

Answers (2)

NinjaNye
NinjaNye

Reputation: 7126

There is a nuget package that allows you to search for strings across multiple properties

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

This will allow the following code...

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

When connected to a sql database, the sql produced will be similar to:

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

Upvotes: 0

JaredPar
JaredPar

Reputation: 754763

The keyword you are looking for is AndAlso. It combines two separate checks into a single one which evaluates to True iff both components are also True

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

Upvotes: 9

Related Questions