如何使用 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/