C# 进行数据行 Select() 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12195952/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:25:37  来源:igfitidea点击:

Making a datarow Select() Query

c#asp.net

提问by Harry47

The thing is i have a data row array which contains ID and parent id as its fields.

问题是我有一个数据行数组,其中包含 ID 和父 ID 作为其字段。

I have used the following code and it returned the correct values ,

我使用了以下代码,它返回了正确的值,

datarow [] dt = datarow.select("  Parent = '0' ");

I want to select only those rows which don't have the '0' as its Parent.I know sql i am not getting how the select function works.

我只想选择那些没有“0”作为其父级的行。我知道 sql 我不知道 select 函数是如何工作的。

采纳答案by LukeHennerley

Try using LINQ - Here is an example to use, I think this is what you wanted:

尝试使用 LINQ - 这是一个使用示例,我认为这就是您想要的:

  DataTable dt = new DataTable("Example");
  dt.Columns.Add("ID", typeof(int));
  dt.Columns.Add("ParentID", typeof(int));
  dt.Rows.Add(1, 0);
  dt.Rows.Add(1, 1);

  var equal0 = dt.Rows.Cast<DataRow>().Where(x => x.Field<int>("ParentID") == 0);
  var notEqual0 = dt.Rows.Cast<DataRow>().Where(x => x.Field<int>("ParentID") != 0);

You get a list of data rows with parent ids which are equal to 0 and a list which doesn't equal 0.

您会得到一个父 ID 等于 0 的数据行列表和一个不等于 0 的列表。

EDIT:

编辑:

I see you are attempting Select - Where is an alternative and does what you want to do. This filters based on a bool condition so you can do where the ParentID is more than x or less than etc.

我看到您正在尝试 Select - Where 是替代方案并执行您想做的事情。此过滤器基于布尔条件,因此您可以在 ParentID 大于 x 或小于等的情况下进行操作。

I hope you find this useful.

希望这个对你有帮助。

回答by Abhishek Mathur

try this ::

尝试这个 ::

DataRow[] dt = dt.Select("Parent != '0'");
dt[0][Your Column Name] = your value;