Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to copy data table to arraylist ...?

How to copy data table to arraylist ...?

Scheduled Pinned Locked Moved C#
data-structuresregextutorialquestion
17 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S SABhatti

    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

    -----

    P Offline
    P Offline
    Pankaj Joshi
    wrote on last edited by
    #3

    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...

    S E 2 Replies Last reply
    0
    • P Pankaj Joshi

      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...

      S Offline
      S Offline
      SABhatti
      wrote on last edited by
      #4

      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()..

      -----

      P 2 Replies Last reply
      0
      • P Pankaj Joshi

        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...

        E Offline
        E Offline
        echuck66
        wrote on last edited by
        #5

        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

        P 1 Reply Last reply
        0
        • S SABhatti

          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()..

          -----

          P Offline
          P Offline
          Pankaj Joshi
          wrote on last edited by
          #6

          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...

          S 1 Reply Last reply
          0
          • E echuck66

            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

            P Offline
            P Offline
            Pankaj Joshi
            wrote on last edited by
            #7

            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...

            1 Reply Last reply
            0
            • S SABhatti

              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()..

              -----

              P Offline
              P Offline
              Pankaj Joshi
              wrote on last edited by
              #8

              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...

              S 1 Reply Last reply
              0
              • P Pankaj Joshi

                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...

                S Offline
                S Offline
                SABhatti
                wrote on last edited by
                #9

                0 means: did not find any row for your criteria 1 means: found 1 row for your criteria

                -----

                P 1 Reply Last reply
                0
                • P Pankaj Joshi

                  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...

                  S Offline
                  S Offline
                  SABhatti
                  wrote on last edited by
                  #10

                  well you can check the number of rows (foundRows.Length) found and show the message accordingly

                  -----

                  1 Reply Last reply
                  0
                  • S SABhatti

                    0 means: did not find any row for your criteria 1 means: found 1 row for your criteria

                    -----

                    P Offline
                    P Offline
                    Pankaj Joshi
                    wrote on last edited by
                    #11

                    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...

                    S 1 Reply Last reply
                    0
                    • P Pankaj Joshi

                      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...

                      S Offline
                      S Offline
                      SABhatti
                      wrote on last edited by
                      #12

                      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.

                      -----

                      P 1 Reply Last reply
                      0
                      • S SABhatti

                        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.

                        -----

                        P Offline
                        P Offline
                        Pankaj Joshi
                        wrote on last edited by
                        #13

                        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...

                        S 1 Reply Last reply
                        0
                        • P Pankaj Joshi

                          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...

                          S Offline
                          S Offline
                          SABhatti
                          wrote on last edited by
                          #14

                          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..

                          -----

                          P 1 Reply Last reply
                          0
                          • S SABhatti

                            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..

                            -----

                            P Offline
                            P Offline
                            Pankaj Joshi
                            wrote on last edited by
                            #15

                            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...

                            S 1 Reply Last reply
                            0
                            • P Pankaj Joshi

                              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...

                              S Offline
                              S Offline
                              SABhatti
                              wrote on last edited by
                              #16

                              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 -----

                              P 1 Reply Last reply
                              0
                              • S SABhatti

                                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 -----

                                P Offline
                                P Offline
                                Pankaj Joshi
                                wrote on last edited by
                                #17

                                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...

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups