在特定行范围内使用数据表选择搜索值 [英] Search value with datatable select in specific row range
问题描述
我正试图从特定的行范围中获取价值。
我尝试了什么:
这是我试过的代码但不知道我应该在哪里使用变量行。
Hi, I am trying to get value from specific row range.
What I have tried:
This is the code i tried but not sure where should i use the variable rows.
var rows = dt.AsEnumerable().Skip(0).Take(15);
string value = "CB123";
DataRow[] result = dt.Select("BOOKCODE like '%" + value + "%'");
//foreach (var col in rows)
//{
if (result.Count() > 0)
{
MessageBox.Show("BookCode Not Found");
//Application.Exit();
}
//}
推荐答案
您可以尝试以下内容。但请注意,结果可能会有所不同,具体取决于数据在datatable
中的排序方式。或者您可以使数据源按照其他成员的建议返回row_number,然后按该变量过滤。
You can try something like below. But be aware that the outcome could be different depending on the way the data being sorted in thedatatable
. Or you can make the datasource to return row_number as suggested by other member and then filter by that variable.
void Test()
{
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
// Here we add five DataRows.
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
table.Rows.Add(22, "xx", "dd", DateTime.Now);
table.Rows.Add(777, "Dilantin 2", "cc", DateTime.Now);
//this should return 10,21,100,22
var resultRange = table.AsEnumerable()
.Where((row, index) => index >= 2 && 5 >= index)
.CopyToDataTable();
//this should return 100
var value = "di";
var result = resultRange.Select("Drug like '%" + value + "%'");
}
c# - 如何根据索引/行号从DataTable中选择行? - 堆栈溢出 [ ^ ]
https ://www.dotnetperls.com/datatable [ ^ ]
你走在正确的轨道上...
You're on the right track...
string bcode = "CB123";
var result = dt.AsEnumerable().Skip(0).Take(15)
.Where(r=> r.Fields<string>("BOOKCODE").Contains(bcode))
.ToList();
foreach(var r in result)
{
//your logic
}
详情请见:
LINQ to DataSet | Microsoft Docs [ ^ ]
编程指南(LINQ to DataSet)| Microsoft Docs [ ^ ]
LINQ to DataSet概述| Microsoft Docs [ ^ ]
For further details, please see:
LINQ to DataSet | Microsoft Docs[^]
Programming Guide (LINQ to DataSet) | Microsoft Docs[^]
LINQ to DataSet Overview | Microsoft Docs[^]
rows.Select ....
或者您可以使用
rows.Select....
Or you can use
ROW_NUMBER
在一个存储过程中写入SQL来为你过滤数据。
我在过滤中从未见过SQL一个IEnumerable。我不确定这是个好主意。
inside a stored proc to write SQL that filters the data for you.
I have never seen SQL in the filtering of an IEnumerable. I'm not sure this is a good idea.
这篇关于在特定行范围内使用数据表选择搜索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!