使用 C#.NET 4.0 LINQ 从数组中删除重复项?

发布于 2024-10-19 07:02:29 字数 448 浏览 7 评论 0原文

我有这个 C# 代码,为服务构建一串逗号分隔的匹配项:

for (m = r.Match(site); m.Success; m = m.NextMatch())
{
     found = found + "," + m.Value.Replace(",", "");
}
return found;

输出如下:aaa,bbb,ccc,aaa,111,111,ccc

现在该代码位于 .NET 4.0 上 如何使用 C# LINQ 删除重复项?

另外,有什么方法可以在不改变顺序的情况下删除重复项吗?

我在另一篇文章中找到了这个示例代码,但不确定如何应用它:

int[] s = { 1, 2, 3, 3, 4}; 
int[] q = s.Distinct().ToArray(); 

谢谢。

I have this c# code that builds a string of comma seperated matches for a service:

for (m = r.Match(site); m.Success; m = m.NextMatch())
{
     found = found + "," + m.Value.Replace(",", "");
}
return found;

Output looks like: aaa,bbb,ccc,aaa,111,111,ccc

Now that code is on .NET 4.0 How can I use C# LINQ to remove duplicates?

Also, Any way to remove duplicates without changing order?

I found this sample code in another post, but not sure exactly how to apply it:

int[] s = { 1, 2, 3, 3, 4}; 
int[] q = s.Distinct().ToArray(); 

Thanks.

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

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

发布评论

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

评论(5

初雪 2024-10-26 07:02:29
string[] s = found.Split(',').Distinct().ToArray()
string[] s = found.Split(',').Distinct().ToArray()
叫思念不要吵 2024-10-26 07:02:29

重写构建结果的代码以直接输出它。

IE。重写这个:

for (m = r.Match(site); m.Success; m = m.NextMatch())
{
     found = found + "," + m.Value.Replace(",", "");
}
return found;

对此:

return (from Match m in r.Matches(site)
        select m.Value.Replace(",", "")).Distinct().ToArray();

这将返回一个数组。如果您仍希望将其作为字符串返回:

return string.Join(", ", (from Match m in r.Matches(site)
        select m.Value.Replace(",", "")).Distinct().ToArray());

您可能能够也可能无法从最后一个代码中删除最后一个 .ToArray(),具体取决于 .NET 运行时版本。 .NET 4.0 string.Join(...) 可以采用 IEnumerable,而以前的版本需要一个数组。

Rewrite the code that builds the result to output it directly.

ie. rewrite this:

for (m = r.Match(site); m.Success; m = m.NextMatch())
{
     found = found + "," + m.Value.Replace(",", "");
}
return found;

To this:

return (from Match m in r.Matches(site)
        select m.Value.Replace(",", "")).Distinct().ToArray();

This will return an array. If you still want it back as a string:

return string.Join(", ", (from Match m in r.Matches(site)
        select m.Value.Replace(",", "")).Distinct().ToArray());

You may or may not be able to remove the last .ToArray() from the last code there depending on the .NET runtime version. .NET 4.0 string.Join(...) can take an IEnumerable<string>, whereas previous versions requires an array.

我的鱼塘能养鲲 2024-10-26 07:02:29

这将返回一串逗号分隔的值,不重复:

var result = string.Join(",",
    r.Matches(site)
        .Cast<Match>()
        .Select(m => m.Value.Replace(",", string.Empty))
        .Distinct()
    );

This will return a string of comma seperated values without duplicates:

var result = string.Join(",",
    r.Matches(site)
        .Cast<Match>()
        .Select(m => m.Value.Replace(",", string.Empty))
        .Distinct()
    );
抱着落日 2024-10-26 07:02:29

这可能是一种可能的解决方案:

var data = new List<string>();
for (m = r.Match(site); m.Success; m = m.NextMatch())
  data.Add(m.Value.Replace(",", ""));
return String.Join(",", data.Distinct().ToArray());

this could be one possible solution:

var data = new List<string>();
for (m = r.Match(site); m.Success; m = m.NextMatch())
  data.Add(m.Value.Replace(",", ""));
return String.Join(",", data.Distinct().ToArray());
薄荷港 2024-10-26 07:02:29

您可以通过单个 LINQ 查询来实现此目的

string strSentence = "aaa,bbb,ccc,aaa,111,111,ccc";
List<string> results = (from w in strSentence.Split(',') select w).Distinct().ToList();

You can achieve this in a single LINQ query

string strSentence = "aaa,bbb,ccc,aaa,111,111,ccc";
List<string> results = (from w in strSentence.Split(',') select w).Distinct().ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文