C# LINQ 从数据表中选择数据行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18272555/
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 DataRow from DataTable
提问by Inkey
I have a DataTable i am trying to do a simple select row that contains a value.
我有一个 DataTable 我正在尝试做一个包含值的简单选择行。
My code
我的代码
var LoginDetails = from myRow in DTOperators.AsEnumerable()
where myRow.Field<string>(0) == UserName
select myRow;
I am trying to check if the string UserName exists at position 0 the rows in the datatable
我试图检查字符串 UserName 是否存在于位置 0 数据表中的行
When i run this query i get a blank datarow back.
当我运行这个查询时,我得到一个空白的数据行。
I have tried to use [] around the position that i want to select.
我试图在我想要选择的位置周围使用 []。
anyone able to see what i am doing wrong.
任何人都能够看到我做错了什么。
采纳答案by Roman Pekar
you have to check if you comparing with right column and check data in your table. this work fine:
您必须检查是否与右列进行比较并检查表中的数据。这项工作很好:
var DTOperators = new DataTable();
var UserName = "test";
DTOperators.Columns.Add("UserName", typeof(string));
DTOperators.Rows.Add("test1");
DTOperators.Rows.Add("test");
var LoginDetails = from myRow in DTOperators.AsEnumerable()
where myRow.Field<string>(0) == UserName
select myRow;
I've got Enumerable with one datarow. You also could try to get data by columnName:
我有一个带有一个数据行的 Enumerable。您也可以尝试通过 columnName 获取数据:
var LoginDetails = DTOperators.Rows
.Cast<DataRow>()
.Where(x => x["UserName"] == UserName).ToList();