C# 将数据行(仅单列)转换为字符串列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10708036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:55:35  来源:igfitidea点击:

Convert datarow(only single column) to a string list

c#linqdatatabledatarow

提问by

Please look at what is wrong? I want to convert datarow to a string list.

请大家看看有什么问题?我想将数据行转换为字符串列表。

 public List<string> GetEmailList()
    {
        // get DataTable dt from somewhere.
        List<DataRow> drlist = dt.AsEnumerable().ToList();
        List<string> sEmail = new List<string>();
        foreach (object str in drlist)
        {
            sEmail.Add(str.ToString()); // exception
        }
        return sEmail; // Ultimately to get a string list.
    }

Thanks for help.

感谢帮助。

采纳答案by ean5533

There's several problems here, but the biggest one is that you're trying to turn an entire rowinto a string, when really you should be trying to turn just a single cellinto a string. You need to reference the first column of that DataRow, which you can do with brackets (like an array).

这里有几个问题,但最大的问题是您试图将整行转换为字符串,而实际上您应该尝试将单个单元格转换为字符串。您需要引用 that 的第一列DataRow,您可以使用括号(如数组)来完成。

Try something like this instead:

试试这样的:

public List<string> GetEmailList()
{
    // get DataTable dt from somewhere.
    List<string> sEmail = new List<string>();
    foreach (DataRow row in dt.Rows)
    {
        sEmail.Add(row[0].ToString());
    }
    return sEmail; // Ultimately to get a string list.
}

回答by Juan Ayala

The Linq way ...

林克方式...

    private static void Main(string[] args)
    {
        var dt = getdt();

        var output = dt
            .Rows
            .Cast<DataRow>()
            .ToList();

        // or a CSV line
        var csv = dt
            .Rows
            .Cast<DataRow>()
            .Aggregate(new StringBuilder(), (sb, dr) => sb.AppendFormat(",{0}", dr[0]))
            .Remove(0, 1);

        Console.WriteLine(csv);
        Console.ReadLine();
    }

    private static DataTable getdt()
    {
        var dc = new DataColumn("column1");
        var dt = new DataTable("table1");
        dt.Columns.Add(dc);
        Enumerable.Range(0, 10)
            .AsParallel()
            .Select(i => string.Format("row {0}", i))
            .ToList()
            .ForEach(s =>
            {
                var dr = dt.NewRow();
                dr[dc] = s;
                dt.Rows.Add(dr);
            });
        return dt;
    }

回答by Necromancer

Here's a one liner I got from ReSharper on how to do this, not sure of the performance implications, just thought I'd share it.

这是我从 ReSharper 那里得到的关于如何做到这一点的一个班轮,不确定对性能的影响,只是想我会分享它。

List<string> companyProxies = Enumerable.Select(
            vendors.Tables[0].AsEnumerable(), vendor => vendor["CompanyName"].ToString()).ToList();

回答by d.popov

Here is how it would be with all additional syntax noise stripped: one-liner.

这是剥离所有附加语法噪声后的情况:单行。

public List<string> GetEmailList()
{
    return dt.AsEnumerable().Select(r => r[0].ToString()).ToList();
}