Hi All,
If we remember foxpro has a classic optimize search function called seek()
on it's in-memory table/cusrsor based on index.
Is there a similar optimize search function avaible in c# datatable.
Which will search in-memory datatable based on datatable index
Regards
Shehzad

Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 5, 2023, 9:55 PM EST
Hello Shehzad,
You can use
DataTable.Select()method.The
Select()method returns an array ofDataRowobjects that match a specified condition.Example :
DataRow[] rows = dt.Select("ID = 2");Or Refer below link
https://stackoverflow.com/a/16767083/3054222
Thanks
Naimish Makwana
Shehzad KhanRank #5,481 · 298 reputationPosted Mar 7, 2023, 2:36 AM EST
Hi Naimish Makwana,
Thanks for sharing,
Actually .Select will scan whole table to select record based on criteria.
I am looking for index based binary search on in-memory datatable.
Regards
Shehzad
Amit MohantyRank #112 · 20K reputationPosted Mar 7, 2023, 4:13 AM EST
Check this
// assume you have a DataTable with a column named "Id" that is indexed DataTable table = new DataTable(); table.Columns.Add("Id", typeof(int)); table.Columns.Add("Name", typeof(string)); table.Rows.Add(1, "Amit"); table.Rows.Add(2, "Alok"); table.Rows.Add(3, "Nagaraj"); table.Rows.Add(4, "Ranjit"); // Create an index on the "Id" column DataView dataView = new DataView(table); dataView.Sort = "Id"; // Perform a binary search for the row with Id = 3 int searchId = 3; int index = dataView.Find(searchId); if (index == -1) Console.WriteLine("Row with Id = {0} not found", searchId); else { DataRow row = table.Rows[index]; Console.WriteLine("Found row with Id = {0} and Name = {1}", row["Id"], row["Name"]); }Shehzad KhanRank #5,481 · 298 reputationPosted Mar 8, 2023, 10:12 PM EST
Hi Amit Mohanty,
Thanks for sharing,
Let me check
Regards
Shehzad