gpt4 book ai didi

c# - 在列表中搜索组合

转载 作者:太空宇宙 更新时间:2023-11-03 19:16:59 25 4
gpt4 key购买 nike

我有以下内容:

public class Address{
public string Number { get; set; }
public string Street { get; set; }
public string Suburb { get; set; }
}

List<Address> MyAddressList = new List<Address>();

Address MyAddress = new Address();
myAddress.Number = "5"
myAddress.Street = "Smith St"
myAddress.Suburb = "Smithsville"
MyAddressList.Add(MyAddress);

Address MyAddress2 = new Address();
myAddress2.Number = "10"
myAddress2.Street = "John St"
myAddress2.Suburb = "Johnsville"
MyAddressList.Add(MyAddress2);

string [] StreetToFind = new string {"Smith St"};
string [] SuburbToFind = new string {"Smithsville"};
string [] secondSuburbToFind = new string {"Johnsville"};

我想搜索此列表并查找值的组合,如果找到该组合,则返回 bool 值。

首先,我可以在 Street Property 中搜索单个值:

bool StreetIsFound = MyAddressList.Select(x => x.Street).Intersect(StreetToFind).Any();

郊区也一样:

bool SuburbIsFind = = MyAddressList.Select(x => x.Suburb).Intersect(SuburbToFind).Any();

但我希望能够同时搜索两者 (bool StreetandSuburbFound)

因此,如果我搜索 StreetToFind 和 SuburbToFind,StreetandSuburbFound 将为真,但如果搜索 StreetToFind 和 secondSuburbToFind,则为假。

最佳答案

您只需调用一个电话 Any 即可完成这一切.像这样

var isFound = MyAddressList.Any(x => StreetToFind.Contains(x.Street) && SuburbToFind.Contains(x.Suburb));

或在查询语法中

var isFound =
(from x in MyAddressList
where StreetToFind.Contains(x.Street)
&& SuburbToFind.Contains(x.Suburb)
select x)
.Any();

关于c# - 在列表中搜索组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15672972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com