How to copy data table to arraylist ...?
-
Hello experts, I having a data table with 10,000 records and I have to traverse to each record to find some specific data. For that I have to loop through 10,000 records. So I decided to copy the match column say [Item Name] into the Array list, So I can use arraylist.contains method which will automatically tells record found or not. And I have to match 5 records every time into the 10,000 records. So my loop is running 50,000 times. How can copy a single column value of data table into the Array list....? :confused:
Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...
-
Hello experts, I having a data table with 10,000 records and I have to traverse to each record to find some specific data. For that I have to loop through 10,000 records. So I decided to copy the match column say [Item Name] into the Array list, So I can use arraylist.contains method which will automatically tells record found or not. And I have to match 5 records every time into the 10,000 records. So my loop is running 50,000 times. How can copy a single column value of data table into the Array list....? :confused:
Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...
why you need array for to do this? you should use DataTable.Select(expression as string) one simple example is here:
DataTable table = DataSet1.Tables["Orders"]; // Presuming the DataTable has a column named Date. string expression; expression = "Date > '1/1/00'"; DataRow[] foundRows; // Use the Select method to find all rows matching the filter. foundRows = table.Select(expression); // Print column 0 of each returned row. for(int i = 0; i < foundRows.Length; i ++) { Console.WriteLine(foundRows[i][0]);
you can change the above code as per your requirement, or look in MSDN for DataTable.Select for more options-----
-
why you need array for to do this? you should use DataTable.Select(expression as string) one simple example is here:
DataTable table = DataSet1.Tables["Orders"]; // Presuming the DataTable has a column named Date. string expression; expression = "Date > '1/1/00'"; DataRow[] foundRows; // Use the Select method to find all rows matching the filter. foundRows = table.Select(expression); // Print column 0 of each returned row. for(int i = 0; i < foundRows.Length; i ++) { Console.WriteLine(foundRows[i][0]);
you can change the above code as per your requirement, or look in MSDN for DataTable.Select for more options-----
First of all thanks for your reply. Actually I have to check 5 or more items each time to the 10,000 records of the datatable. Your solution is good but it will work only for one time I have to change expression = "ItemName = 'mobile'"; 5 times. Like expression = "ItemName = 'mobile'"; expression = "ItemName = 'tv'"; expression = "ItemName = 'radio'"; expression = "ItemName = 'computer'"; SO how I do that...?
Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...
-
First of all thanks for your reply. Actually I have to check 5 or more items each time to the 10,000 records of the datatable. Your solution is good but it will work only for one time I have to change expression = "ItemName = 'mobile'"; 5 times. Like expression = "ItemName = 'mobile'"; expression = "ItemName = 'tv'"; expression = "ItemName = 'radio'"; expression = "ItemName = 'computer'"; SO how I do that...?
Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...
well you should be able to add multiple expressions using and/or e.g.
expression = "ItemName = 'mobile' or ItemName = 'tv' or ItemName = 'radio' or ItemName = 'computer'";
you can try in also like "ItemName in('mobile','tv', 'radio', 'computer')" and if you have any primary key in the table then use DataTable.Rows.Find instead of .Select()..-----
-
First of all thanks for your reply. Actually I have to check 5 or more items each time to the 10,000 records of the datatable. Your solution is good but it will work only for one time I have to change expression = "ItemName = 'mobile'"; 5 times. Like expression = "ItemName = 'mobile'"; expression = "ItemName = 'tv'"; expression = "ItemName = 'radio'"; expression = "ItemName = 'computer'"; SO how I do that...?
Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...
Why not create separate DataRow arrays for each expression if you want to segregate them, or use OR in your select string if you want to group them all together? For example (using expresion segregation): DataTable dt = ...(your data table definition) DataRow[] drMobile = dt.Select("ItemName = 'mobile'"); DataRow[] drTV = dt.Select("ItemName = 'tv'"); DataRow[] drRadio = dt.Select("ItemName = 'radio'"); DataRow[] drComputer = dt.Select("ItemName = 'computer'"); or (grouping them all together): DataTable dt = ...(your data table definition) DataRow[] drows = dt.Select("ItemName = 'mobile' OR ItemName = 'tv' OR ItemName = 'radio' OR ItemName = 'computer'"); By the way, I don't recommend using ArrayList... instead, use Generics, for example, instead of using: ArrayList al = new ArrayList(); string v1 = "some text"; string v2 = "some more text"; string v3 = "even more text"; // you get the point... al.Add(v1); al.Add(v2); al.Add(v3); // etc. and then casting the content back to a string variable: string v1Str = (string)al[0]; ... Use Generics: List<string> al = new List<string>(); string v1 = "some text"; string v2 = "some more text"; string v3 = "even more text"; al.Add(v1); al.Add(v2); al.Add(v3); // etc. now, you don't have to cast the values back to a string when you retrieve them: string v1Str = al[0]; string v2Str = al[1]; ... Depending on what you are storing in the ArrayList, the cast operation can be very costly in terms of performance. Microsoft doesn't recommend using the ArrayList in .NET 2.0 and higher. -- modified at 22:03 Sunday 18th November, 2007
-
well you should be able to add multiple expressions using and/or e.g.
expression = "ItemName = 'mobile' or ItemName = 'tv' or ItemName = 'radio' or ItemName = 'computer'";
you can try in also like "ItemName in('mobile','tv', 'radio', 'computer')" and if you have any primary key in the table then use DataTable.Rows.Find instead of .Select()..-----
Dear SABhatti, Actually I want to know weather or not any item is mismatch. Suppose I have 5 items and 2 items are missing then the functions should say their are 2 items are missing.
Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...
-
Why not create separate DataRow arrays for each expression if you want to segregate them, or use OR in your select string if you want to group them all together? For example (using expresion segregation): DataTable dt = ...(your data table definition) DataRow[] drMobile = dt.Select("ItemName = 'mobile'"); DataRow[] drTV = dt.Select("ItemName = 'tv'"); DataRow[] drRadio = dt.Select("ItemName = 'radio'"); DataRow[] drComputer = dt.Select("ItemName = 'computer'"); or (grouping them all together): DataTable dt = ...(your data table definition) DataRow[] drows = dt.Select("ItemName = 'mobile' OR ItemName = 'tv' OR ItemName = 'radio' OR ItemName = 'computer'"); By the way, I don't recommend using ArrayList... instead, use Generics, for example, instead of using: ArrayList al = new ArrayList(); string v1 = "some text"; string v2 = "some more text"; string v3 = "even more text"; // you get the point... al.Add(v1); al.Add(v2); al.Add(v3); // etc. and then casting the content back to a string variable: string v1Str = (string)al[0]; ... Use Generics: List<string> al = new List<string>(); string v1 = "some text"; string v2 = "some more text"; string v3 = "even more text"; al.Add(v1); al.Add(v2); al.Add(v3); // etc. now, you don't have to cast the values back to a string when you retrieve them: string v1Str = al[0]; string v2Str = al[1]; ... Depending on what you are storing in the ArrayList, the cast operation can be very costly in terms of performance. Microsoft doesn't recommend using the ArrayList in .NET 2.0 and higher. -- modified at 22:03 Sunday 18th November, 2007
Thank you very much for your suggestion. Actually I have a data table with 10thousand records and now I have 4-5 items and I want to found any of item is missing or not ..? for that I have to traverse through data table, So I use array list which is having contains method which make traversing fast. Please give a good solution to resolve this issue. Also thanks for the guidance of using list instead of array list.
Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...
-
well you should be able to add multiple expressions using and/or e.g.
expression = "ItemName = 'mobile' or ItemName = 'tv' or ItemName = 'radio' or ItemName = 'computer'";
you can try in also like "ItemName in('mobile','tv', 'radio', 'computer')" and if you have any primary key in the table then use DataTable.Rows.Find instead of .Select()..-----
Sir, foundRows.Length returns what...? 0 means...? 1 means...?:confused:
Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...
-
Sir, foundRows.Length returns what...? 0 means...? 1 means...?:confused:
Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...
-
Dear SABhatti, Actually I want to know weather or not any item is mismatch. Suppose I have 5 items and 2 items are missing then the functions should say their are 2 items are missing.
Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...
-
0 means: did not find any row for your criteria 1 means: found 1 row for your criteria
-----
Ok. Then it is difficult to find which item is missing...?
Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...
-
Ok. Then it is difficult to find which item is missing...?
Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...
-
well I am sorry, but are you a programmer? if you want that then you can find all rows one by one and see what you are missing. Or you can compare the results again for your criteria and see what you are missing.
-----
YES YES YES !!!! :omg: I am a programmer , and facing problems so ask this question. Why are you asking that...? Any prob...?
Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...
-
YES YES YES !!!! :omg: I am a programmer , and facing problems so ask this question. Why are you asking that...? Any prob...?
Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...
well, sorry if you are offended. but normally people ask for guidance/hints in the forums and you can't even think/do this simple thing after you have all the code to find the results... all code was there and now you just had to think a very little to do your task but you posted the next question without thinking ... if you really want to learn then please spend some time in making your logic on paper before start coding..
-----
-
well, sorry if you are offended. but normally people ask for guidance/hints in the forums and you can't even think/do this simple thing after you have all the code to find the results... all code was there and now you just had to think a very little to do your task but you posted the next question without thinking ... if you really want to learn then please spend some time in making your logic on paper before start coding..
-----
Yes, sir you are right. Actually what is happening, I am having more than 23,000 items and I don't want to traverse. I search a lot but no good solution is found. And if I am having 10 records in a new order then I have to traverse through the 23thousand records so I ask a solution. The solution you provide is good but I unable to understand how I can found the unmatched items...? At the end of all.... I am very very sorry if I hurt you any way. :rose:
Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...
-
Yes, sir you are right. Actually what is happening, I am having more than 23,000 items and I don't want to traverse. I search a lot but no good solution is found. And if I am having 10 records in a new order then I have to traverse through the 23thousand records so I ask a solution. The solution you provide is good but I unable to understand how I can found the unmatched items...? At the end of all.... I am very very sorry if I hurt you any way. :rose:
Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...
well without going in detail here is the quick fix for you:
// flags to check if found bool testMatched, test1Matched, test2Matched, test4Matched; // by default none found testMatched = false; test1Matched = false; test2Matched = false; test4Matched = false; foundRows = datatable.Select("itemName='test' or itemName='test1' or itemName='test2' or itemName='test4'"); // if any row found if(foundRows.Length > 0) { // loop through all found rows for(int i=0; i hope this will help you -----
-
well without going in detail here is the quick fix for you:
// flags to check if found bool testMatched, test1Matched, test2Matched, test4Matched; // by default none found testMatched = false; test1Matched = false; test2Matched = false; test4Matched = false; foundRows = datatable.Select("itemName='test' or itemName='test1' or itemName='test2' or itemName='test4'"); // if any row found if(foundRows.Length > 0) { // loop through all found rows for(int i=0; i hope this will help you -----
Thanks for your reply sir... I also create a logic can you check it for me? //Filling the datatable dataTableItems.... ///Create the generic object List listNoOfItems = new List(); //Adding the search item into the list listNoOfItems.Add("Item1"); listNoOfItems.Add("Item2"); for (int listIndex = 0; listIndex <= listNoOfItems.Count - 1; listIndex++) { DataRow[] foundRows; //Finding the rows.... foundRows = dataTableItems.Select("ItemName = '" + listNoOfItems[listIndex] + "'"); if (foundRows.Length <= 0) { MessageBox.Show("Not found " + listNoOfItems[listIndex]); } else { MessageBox.Show("Found " + foundRows[0]["ItemName"].ToString()); } } Will this code is OK...?
Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...