C# Linq:选择数据表列中的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1880163/
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
Linq : select value in a datatable column
提问by ianbeks
How do you use LINQ (C#)
to select the value in a particular column for a particular row in a datatable
. The equivalent SQL
would be:
您如何LINQ (C#)
用于为 .csv 文件中的特定行选择特定列中的值datatable
。相当于SQL
:
select NAME from TABLE where ID = 0
Thanks in advance.
提前致谢。
采纳答案by James
var name = from r in MyTable
where r.ID == 0
select r.Name;
If the row is unique then you could even just do:
如果该行是唯一的,那么您甚至可以这样做:
var row = DataContext.MyTable.SingleOrDefault(r => r.ID == 0);
var name = row != null ? row.Name : String.Empty;
回答by Chris
var x = from row in table
where row.ID == 0
select row
Supposing you have a DataTable that knows about the rows, other wise you'll need to use the row index:
假设您有一个知道行的 DataTable,否则您需要使用行索引:
where row[rowNumber] == 0
In this instance you'd also want to use the select to place the row data into an anonymous class or a preprepared class (if you want to pass it to another method)
在这种情况下,您还想使用 select 将行数据放入匿名类或准备好的类中(如果您想将其传递给另一个方法)
回答by fyjham
I notice others have given the non-lambda syntax so just to have this complete I'll put in the lambda syntax equivalent:
我注意到其他人已经给出了非 lambda 语法,所以为了完成这个,我将放入等效的 lambda 语法:
Non-lambda (as per James's post):
非 lambda(根据 James 的帖子):
var name = from i in DataContext.MyTable
where i.ID == 0
select i.Name
Equivalent lambda syntax:
等效的 lambda 语法:
var name = DataContext.MyTable.Where(i => i.ID == 0)
.Select(i => new { Name = i.Name });
There's not really much practical difference, just personal opinion on which you prefer.
没有太大的实际区别,只是您更喜欢的个人意见。
回答by ianbeks
Thanks for your answers. I didn't understand what type of object "MyTable" was (in your answers) and the following code gave me the error shown below.
感谢您的回答。我不明白“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
找不到源类型“System.Data.DataTable”的查询模式的实现。找不到“哪里”
So I continued my googling and found something that does work:
所以我继续我的谷歌搜索并找到了一些有用的东西:
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>();
What do you think?
你怎么认为?
回答by Krishna Ballavi Rath
var name = from DataRow dr in tblClassCode.Rows where (long)dr["ID"] == Convert.ToInt32(i) select (int)dr["Name"]).FirstOrDefault().ToString()
回答by Ashraf Abusada
Use linq and set the data table as Enumerable and select the fields from the data table field that matches what you are looking for.
使用 linq 并将数据表设置为可枚举,然后从数据表字段中选择与您要查找的内容匹配的字段。
Example
例子
I want to get the currency Id and currency Name from the currency table where currency is local currency, and assign the currency id and name to a text boxes on the form:
我想从货币表中获取货币 ID 和货币名称,其中货币是本地货币,并将货币 ID 和名称分配给表单上的文本框:
DataTable dt = curData.loadCurrency();
var curId = from c in dt.AsEnumerable()
where c.Field<bool>("LocalCurrency") == true
select c.Field<int>("CURID");
foreach (int cid in curId)
{
txtCURID.Text = cid.ToString();
}
var curName = from c in dt.AsEnumerable()
where c.Field<bool>("LocalCurrency") == true
select c.Field<string>("CurName");
foreach (string cName in curName)
{
txtCurrency.Text = cName.ToString();
}
回答by mbadeveloper
If the return value is string and you need to search by Id you can use:
如果返回值是字符串并且您需要按 Id 搜索,则可以使用:
string name = datatable.AsEnumerable().Where(row => Convert.ToInt32(row["Id"]) == Id).Select(row => row.Field<string>("name")).ToString();
or using generic variable:
或使用通用变量:
var name = datatable.AsEnumerable().Where(row => Convert.ToInt32(row["Id"]) == Id).Select(row => row.Field<string>("name"));