以 IEnumerable 形式返回列值

发布于 2024-08-07 09:02:33 字数 379 浏览 6 评论 0原文

我有这个代码工作:

public IEnumerable<string> GetEmpNames()
{
    var cmd = SqlCommand("select [EmpName] from [dbo].[Emp]");
    using (var rdr = cmd.ExecuteReader())
        while (rdr.Read())
            yield return (string) rdr["EmpName"];
}

但是,我想知道是否有更好的(LINQish)方法,而不必诉诸收益回报。 (并且 LINQ to SQL 不是一个选项:))

I have this code working:

public IEnumerable<string> GetEmpNames()
{
    var cmd = SqlCommand("select [EmpName] from [dbo].[Emp]");
    using (var rdr = cmd.ExecuteReader())
        while (rdr.Read())
            yield return (string) rdr["EmpName"];
}

However, I'm wondering if there's a better (LINQish) way, not having to resort to yield return. (And LINQ to SQL is not an option :) )

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

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

发布评论

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

评论(2

明月松间行 2024-08-14 09:02:33
IEnumerable<string> result = DataContext.ExecuteQuery<string>(sqlstring)

http://msdn.microsoft.com/en-us/library/bb361109.aspx

IEnumerable<string> result = DataContext.ExecuteQuery<string>(sqlstring)

http://msdn.microsoft.com/en-us/library/bb361109.aspx

我偏爱纯白色 2024-08-14 09:02:33

你不应该那样做!您的阅读器需要尽快关闭。您不想在枚举期间保持其打开状态。最好只创建一个显式列表,然后返回该列表。

var cmd = SqlCommand("select [EmpName] from [dbo].[Emp]");
List<string> results = new List<string>();
using (var rdr = cmd.ExecuteReader()) {
    while (rdr.Read())
        results.Add((string) rdr["EmpName"]);
}
return results;

您可以通过强制转换在 DataReader 上使用 Linq 表达式:

using (var rdr = cmd.ExecuteReader()) {
    results = (from row in rdr.Cast<DbDataRecord>()
               select (string)row["EmpName"]).ToList();
}

但请注意,您需要调用 ToList(),否则当您尝试枚举时会收到错误,因为读取器已关闭。

编辑

对于 DataReader 打开时实际执行的操作,评论中似乎存在一些混乱。 来自 MSDN

当使用 SqlDataReader 时,
关联的 SqlConnection 正忙
为 SqlDataReader 提供服务,并且没有
可以执行其他操作
除了关闭之外的 SqlConnection
它。直到关闭为止都是这种情况
调用 SqlDataReader 的方法。
例如,您无法检索
输出参数直到调用之后
关闭。

因此,您应该尽快关闭它以释放连接。

You should not do that! Your reader needs to be closed as soon as possible. you do not want to keep it open for the duration of the enumeration. It is better to just create an explicit list, and return that.

var cmd = SqlCommand("select [EmpName] from [dbo].[Emp]");
List<string> results = new List<string>();
using (var rdr = cmd.ExecuteReader()) {
    while (rdr.Read())
        results.Add((string) rdr["EmpName"]);
}
return results;

You can use Linq expressions on a DataReader by casting it:

using (var rdr = cmd.ExecuteReader()) {
    results = (from row in rdr.Cast<DbDataRecord>()
               select (string)row["EmpName"]).ToList();
}

But notice that you need to call ToList(), or you will get an error when you try to enumerate because the reader has already been closed.

Edit

There seems to be some confusion in the comments about what a DataReader actually does when it's open. From MSDN:

While the SqlDataReader is being used,
the associated SqlConnection is busy
serving the SqlDataReader, and no
other operations can be performed on
the SqlConnection other than closing
it. This is the case until the Close
method of the SqlDataReader is called.
For example, you cannot retrieve
output parameters until after you call
Close.

Therefore you should close it as soon as possible to free up the connection.

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