3

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)

0

2 Answers 2

9

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)
0
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.