简体   繁体   English

带有列表的实体框架查询

[英]Entity Framework query with a list

What I got is an address column with varchars in a database which looks like this:我得到的是数据库中带有 varchars 的地址列,如下所示:

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.我正在考虑使用一个附加列表,在该列表中我将通过邮政编码列表添加一个带有 foreach 循环的单个结果。

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.请记住,存在误报,例如,如果您的邮政编码为1234并且您的地址为邮政编码12345 ,那么您将遇到冲突。 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:使用 LINQ.Any 方法:

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

And the output will be: output 将是:

foreach (var item in result) 
{
  Console.WriteLine(item);
}

which should yield:这应该产生:

Elmstreet / 12345 / USA
Time Square / 77777 / USA

Welcome to SO.欢迎来到 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:为此,您可以使用.Any这里的任何方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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