By cnndlc

2019-11-08 14:13:02 8 Comments

What I got is an address column with varchars in a database which looks like this:

Elmstreet / 12345 / USA

It contains Street, postal code and country. Unfortunately it's an existing database and I'm not allowed to make any changes there. So now I need a result set based on a list of postal codes.

The easiest way to get it is this:

string postalCode = "12345";
addresses = addresses.Where(x => x.Address.Contains(postalCode));

But as I have a list of postal codes I did this:

addresses = addresses.Where(x => postalCodes.Contains(x.Address));

But I don't get any results. And it's obvious after thinking about what the code does.

I was thinking of working with an additional list in which I would add the single results with a foreach loop through the postal code list.

But is there maybe an easier and better way to do that?

Thanks a lot.


@Flater 2019-11-08 14:29:01

addresses = addresses.Where(x => postalCodes.Contains(x.Address));

What you asked here (as you've discovered) is whether the list of postal codes contains this entire address. But that's not what you want.

What you want to check is if any of the postal codes can be found in an address, and only get the addresses for which this is true. The following approach works:

addresses = addresses.Where(x => 
                          postalCodes.Any(postalCode =>

To put it into words: give me all addresses where the address string contains any of these postal codes.

As an aside:

Keep in mind that there are false positives, for example if you have postal code 1234 and you have addresses with postal code 12345, you're going to get conflicts. One way to avoid this is to include the / delimiter from the address field in your postcode:

postalCodes = postalCodes.Select(pc => $"/ {pc} /");

// and then the rest of the code as before

This will prevent most (if not all) false positives.

@cnndlc 2019-11-08 15:18:07

Hi Flater, yes this is what I wanted. Any() is the solution. Thanks a lot.

@Angelo 2019-11-08 14:28:04

Assuming this is your addresses list structure:

var addresses = new List<string>
  "Elmstreet / 12345 / USA",
  "Hollywood / 67890 / USA",
  "Time Square / 77777 / USA"

and your postalCodes list:

var postalCodes = new List<string>

Use the LINQ .Any method:

var result = addresses.Where(a => postalCodes.Any(p => a.Contains(p))).ToList();

And the output will be:

foreach (var item in result) 

which should yield:

Elmstreet / 12345 / USA
Time Square / 77777 / USA

@cnndlc 2019-11-08 16:01:02

this answers my question as well. Thanks.

Related Questions

Sponsored Content

21 Answered Questions

28 Answered Questions

[SOLVED] How do I concatenate two lists in Python?

12 Answered Questions

[SOLVED] Getting the last element of a list

  • 2009-05-30 19:28:53
  • Janusz
  • 2158252 View
  • 2098 Score
  • 12 Answer
  • Tags:   python list indexing

9 Answered Questions

[SOLVED] How do I get the number of elements in a list?

  • 2009-11-11 00:30:54
  • y2k
  • 3279998 View
  • 1953 Score
  • 9 Answer
  • Tags:   python list

62 Answered Questions

[SOLVED] How do you split a list into evenly sized chunks?

30 Answered Questions

[SOLVED] Finding the index of an item in a list

  • 2008-10-07 01:39:38
  • Eugene M
  • 3951023 View
  • 3237 Score
  • 30 Answer
  • Tags:   python list indexing

40 Answered Questions

[SOLVED] How to make a flat list out of list of lists?

27 Answered Questions

[SOLVED] How do I check if a list is empty?

  • 2008-09-10 06:20:11
  • Ray
  • 2933261 View
  • 3234 Score
  • 27 Answer
  • Tags:   python list

27 Answered Questions

[SOLVED] Why not inherit from List<T>?

20 Answered Questions

Sponsored Content