简体   繁体   中英

Entity Framework query with a list

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.

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 =>
                              x.Address.Contains(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.

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>
{
  "12345",
  "77777"
}

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) 
{
  Console.WriteLine(item);
}

which should yield:

Elmstreet / 12345 / USA
Time Square / 77777 / USA

Welcome to SO. If I understood correctly you want to get all addresses that contain at least one postcode from the list of postcodes. For this you could use LINQ .Any method here:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM