c# - 林克 : select value in a datatable column

标签 c# linq datatable

如何使用 LINQ (C#)datatable 中的特定行选择特定列中的值。等效的 SQL 将是:

select NAME from TABLE where ID = 0

提前致谢。

最佳答案

感谢您的回答。我不明白“MyTable”是什么类型的对象(在您的回答中),下面的代码给了我如下所示的错误。

DataTable dt = ds.Tables[0];
var name = from r in dt
           where r.ID == 0
           select r.Name;

Could not find an implementation of the query pattern for source type 'System.Data.DataTable'. 'Where' not found

所以我继续谷歌搜索并找到了一些有用的东西:

var rowColl = ds.Tables[0].AsEnumerable();
string name = (from r in rowColl
              where r.Field<int>("ID") == 0
              select r.Field<string>("NAME")).First<string>();

你怎么看?

关于c# - 林克 : select value in a datatable column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1880163/

相关文章:

c# - 嵌套查询 MVC LINQ

C# 以更好的时间复杂度聚合

c# - 连接字符串时处理 LINQ 中的空列

c# - 构造函数如何设置类成员的默认值?

c# - 如何使用 XPathDocument 和 C# 获取节点数?

c# - 使用 TCP 发送和接收纯文本

c# - linq "System.NotSupportedException"有错误

c# - 创建数据表

jsf - :selectOneMenu in p:dataTable doesn't submit its value

c# - 从其他表单调用时,DataGridView.DataSource 返回空 DataTable