且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从LINQ中的字符串列表中生成CSV

更新时间:2023-12-03 19:16:28

没有任何一个LINQ可以使用LINQ。请注意,您可以使用Aggregate方法来执行此操作,但是您将使用字符串连接而不是StringBuilder。如果你将经常做的事情,我会考虑添加一个扩展方法:

There are not any one liners to do this with LINQ. Note that you can use the Aggregate method to do this, but you will be using string concatenation instead of a StringBuilder. I would consider adding an extension method for this if it something you will be doing often:

    public static string ToCsv<T>(this IEnumerable<T> source)
    {
        if (source == null) throw new ArgumentNullException("source");
        return string.Join(",", source.Select(s => s.ToString()).ToArray());
    }