【问题标题】:What is the best way to load a column from a dataset (.net) into a string[]?将数据集(.net)中的列加载到字符串 [] 中的最佳方法是什么?
【发布时间】:2009-06-18 10:23:58
【问题描述】:

ASP.NET/C# 3.0

我有一个具有以下签名的方法:

public static void SendEmail(string sFrom, string sSubject, string sBody, params string[] sAddresses)

我需要遍历一个数据集来检查 column2 的值。如果 column2 的值为 True,则应将 column1 中的字符串值添加到 sAddresses 字符串[]。

我知道的唯一方法是在每次迭代时调整 string[] +1 的大小。这听起来效率不高。雪莉有更好的方法。

谢谢! 凯文

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    使用List<string> 并在那里添加项目。完成后,调用 ToArray 获取字符串数组。

    【讨论】:

      【解决方案2】:

      默认情况下我会跳转到 Linq。不过,它可能不是最好的:

      string[] addresses = (from row in table.Rows
                            where ((bool)row[2])
                            select (string)row[1]).ToArray()
      

      【讨论】:

        猜你喜欢
        • 2011-03-09
        • 1970-01-01
        • 2010-12-30
        • 1970-01-01
        • 1970-01-01
        • 2017-04-15
        • 2017-06-24
        • 2019-01-31
        • 1970-01-01
        相关资源
        最近更新 更多