最简单的合取(交集)代码

发布于 2024-12-02 22:13:50 字数 488 浏览 0 评论 0原文

我有两个数组,我想要知道它们是否有共同元素的最简单方法。所以其实这个问题还得问。

string[] countries1 = new string[] { "USA", "Uruguay", "India", "UK"};  
string[] countries2 = new string[] { "Urguay", "Argentina", "Brasil", "Chile" };  
foreach (string country in countries1)
    if (countries2.Contains(country))  
        return true;  
return false;
  1. 让我知道 country1 国家/地区是否也在 country2 数组中的最简单的 linq 查询是什么?
  2. 将返回每个重复国家/地区的数组的最简单的 linq 查询是什么?

I have two arrays and I wnat the simplest way of knowing if they have elements in common. So actually this question have to questions.

string[] countries1 = new string[] { "USA", "Uruguay", "India", "UK"};  
string[] countries2 = new string[] { "Urguay", "Argentina", "Brasil", "Chile" };  
foreach (string country in countries1)
    if (countries2.Contains(country))  
        return true;  
return false;
  1. What's the simplest linq query that will let me know if any of the country1 countries is also in the country2 array?
  2. What's the simplest linq query that will return an array of every repeated country?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

辞慾 2024-12-09 22:13:50

1) var isIntersection =countries1.Intersect(countries2).Any();

2) var intersectedCountries =countries1.Intersect(countries2);

1) var isIntersection = countries1.Intersect(countries2).Any();

2) var intersectedCountries = countries1.Intersect(countries2);

Oo萌小芽oO 2024-12-09 22:13:50

对于国家 1 和 2 之间的交集:

countries1.Intersect(countries2).ToArray()

For intersection between countries 1 and 2:

countries1.Intersect(countries2).ToArray()

浅笑依然 2024-12-09 22:13:50

阿迪尔森的回答涵盖了您的问题 #2 和问题 #1

最简单的 linq 查询是什么,可以让我知道country1 中的任何一个国家/地区是否也在country2 数组中?

您会这​​样做:

countries1.Intersect(countries2).Any();

.Any() 将在匹配的第一个实例上返回 true,而 .Count().ToArray() > 将迭代整个列表。

Adilson's answer covers your question #2, and for question #1

What's the simplest linq query that will let me know if any of the country1 countries is also in the country2 array?

You would do:

countries1.Intersect(countries2).Any();

The .Any() will return true on the first instance of a match, whereas .Count() or .ToArray() will iterate the entire list.

日记撕了你也走了 2024-12-09 22:13:50

使用 LINQ:

var commonCountries = countries1.Intersect(countries2);

if (commonCountries.Any())
    // There are common countries.

但是,这不会考虑字符串大小写等。您可能想要做的是将一个快速 IEqualityComparer 组合在一起:

public class OrdinalStringComparer : IEqualityComparer<string>
{
    public bool Equals(string s1, string s2)
    {
        return string.Equals(s1, s2, StringComparison.OrdinalCultureIgnoreCase);
    }
    public int GetHashCode(string str)
    {
        return (str == null) ? 0 : str.GetHashCode();
    }
}

然后将其与您的 Intersect 一起传递调用:

var commonCountries = countries1.Intersect(countries2, new OrdinalStringComparer());

With LINQ:

var commonCountries = countries1.Intersect(countries2);

if (commonCountries.Any())
    // There are common countries.

But, this doesn't take into consideration string casing etc. What you may want to do, is chuck together a quick IEqualityComparer<string>:

public class OrdinalStringComparer : IEqualityComparer<string>
{
    public bool Equals(string s1, string s2)
    {
        return string.Equals(s1, s2, StringComparison.OrdinalCultureIgnoreCase);
    }
    public int GetHashCode(string str)
    {
        return (str == null) ? 0 : str.GetHashCode();
    }
}

Then pass that in with your Intersect call:

var commonCountries = countries1.Intersect(countries2, new OrdinalStringComparer());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文