如何使用扩展方法Join()?

发布于 2024-08-09 00:42:57 字数 302 浏览 8 评论 0 原文

我可以理解 string.Join( )

  var stringies1 =new [] {"Blood", "is", "Thicker", "Than", "Water" };
  var zoin = string.Join("|", stringies1);

它与扩展方法 Join() 有什么不同?

我的意思是 stringies1.Join(IEnumerable

I can understand the string.Join( )

  var stringies1 =new [] {"Blood", "is", "Thicker", "Than", "Water" };
  var zoin = string.Join("|", stringies1);

How does it differ from extension method Join() ?

I mean stringies1.Join(IEnumerable<T Result......)

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

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

发布评论

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

评论(3

魂归处 2024-08-16 00:42:57

您所指的扩展方法 Enumerable.Join 用于连接集合,这意味着您将它们并排放置并尝试将项目与项目进行匹配,从而产生结果。可以将其想象为将电话簿与您的聚会姓名列表进行匹配,以查找列表中所有姓名的电话号码。

因此,不可以,扩展方法 Join 不能用于将它们依次放置在一个数组中并将它们组合成一个长数组。

但是,还有一种替代方法,您可以使用扩展方法 Enumerable.Concat 在同一个班级。

此方法不仅仅对数组进行操作,而是对所有实现 IEnumerable 的集合类型进行操作,因此还会生成另一个 IEnumerable 作为其结果。然后,您可以使用 Enumerable.ToArray 将此结果转换为数组。

因此,您将使用以下代码:

var stringies1 = new [] {"Blood", "is", "Thicker", "Than", "Water" };
var stringies2 = new [] { "and", "so", "is", "wine" };

var stringies3 = stringies1.Concat(stringies2).ToArray();
// stringies3 now contains "Blood", "is", ... and "and", "so", "is", ...

假设您正在使用 .NET 3.5,并且文件顶部的 using-list 中有以下内容:

using System.Linq;

另一方面,如果您(或其他人找到此答案)不使用 .NET 3.5,您需要一些手动代码。这是一个可以帮助您的简单通用方法(假设是 .NET 2.0):

public static T[] Concat<T>(T[] firstSource, T[] secondSource,
    params T[] restOfSources)
{
    // omitted validation of non-null arguments, etc.

    Int32 totalLength = firstSource.Length + secondSource.Length;
    foreach (T[] source in restOfSources)
        totalLength += source.Length;

    T[] result = new T[totalLength];
    Int32 currentIndex = 0;

    Array.Copy(firstSource, 0, result, currentIndex, firstSource.Length);
    currentIndex += firstSource.Length;

    Array.Copy(secondSource, 0, result, currentIndex, secondSource.Length);
    currentIndex += secondSource.Length;

    foreach (T[] source in restOfSources)
    {
        Array.Copy(source, 0, result, currentIndex, source.Length);
        currentIndex += source.Length;
    }

    return result;
}

可以像这样使用:

var stringies1 = ...
var stringies2 = ...

var stringies3 = YourClass.Concat(stringies1, stringies2);

The extension method you're referring to, Enumerable.Join is for joining collections, which means you place them up side by side and try to match items against items, producing results. Think of it like matching the phone book with your list of names for a party, to find the phone number for all the names you have on your list.

So, no, the extension method Join can not be used to place them one array after each other and combine them to one long array.

There is an alternative however, you can use the extension method Enumerable.Concat in the same class.

This method does not operate on/with only arrays, but on all collection types that implement IEnumerable<T>, and will thus also produce another IEnumerable<T> as its result. This result you can then convert to an array using Enumerable.ToArray.

Thus, you would use the following code:

var stringies1 = new [] {"Blood", "is", "Thicker", "Than", "Water" };
var stringies2 = new [] { "and", "so", "is", "wine" };

var stringies3 = stringies1.Concat(stringies2).ToArray();
// stringies3 now contains "Blood", "is", ... and "and", "so", "is", ...

This assumes you're using .NET 3.5 and have the following in the using-list at the top of your file:

using System.Linq;

If, on the other hand, you (or someone else finding this answer) are not using .NET 3.5, you need some manual code. Here's a simple generic method (assumes .NET 2.0) that can help you:

public static T[] Concat<T>(T[] firstSource, T[] secondSource,
    params T[] restOfSources)
{
    // omitted validation of non-null arguments, etc.

    Int32 totalLength = firstSource.Length + secondSource.Length;
    foreach (T[] source in restOfSources)
        totalLength += source.Length;

    T[] result = new T[totalLength];
    Int32 currentIndex = 0;

    Array.Copy(firstSource, 0, result, currentIndex, firstSource.Length);
    currentIndex += firstSource.Length;

    Array.Copy(secondSource, 0, result, currentIndex, secondSource.Length);
    currentIndex += secondSource.Length;

    foreach (T[] source in restOfSources)
    {
        Array.Copy(source, 0, result, currentIndex, source.Length);
        currentIndex += source.Length;
    }

    return result;
}

This can be used like this:

var stringies1 = ...
var stringies2 = ...

var stringies3 = YourClass.Concat(stringies1, stringies2);
忆悲凉 2024-08-16 00:42:57

我假设通过扩展方法,您指的是 Enumerable.Join 方法? string.Join 和 IEnumerable.Join 是两种截然不同的方法:

  • string.Join 将获取一个字符串数组,使用某种分隔符将它们连接在一起,然后返回结果字符串。
  • Enumerable.Join 将作用于两个集合的方式与 SQL 中的 JOIN 操作非常相似

I assume that by the extension method, you are referring to the Enumerable.Join method? The string.Join an IEnumerable.Join are two rather different methods:

  • string.Join will take an array of strings, join them together using some separator, and return the resulting string.
  • Enumerable.Join will act on two collections much in the same way as a JOIN operation in SQL does
︶葆Ⅱㄣ 2024-08-16 00:42:57

相同的功能采用不同的方法来提高可读性,并在适当的时候减少代码。

Same function different approach for readability, and less code when appropriate.

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