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. Modify Collection using LINQ

Modify Collection using LINQ

Scheduled Pinned Locked Moved C#
questiontutorialcsharpdatabaselinq
4 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.
  • U Offline
    U Offline
    uusheikh
    wrote on last edited by
    #1

    I have a simple question, but i don't know how to do it. I have a collection of data, and would like to query it using LINQ. Then, i want to modify the resultant collection, which updates the original collection For example i have the following collection int[] data = { 0, 1, 2, 3, 4, 5 }; I want to select all data larger than 3 (thus 4 and 5), and then modify them by multiplying them with 10. My final collection should look like this. (This is a simple example, in reality, i will be using a complex object) data = {0, 1, 2, 3, 40 , 50}; I tried this; but doesn't update the original collection. Does ToList() return a copy of the collection? How do i go about doing this?

    int[] data = { 0, 1, 2, 3, 4, 5 };

            var a = from n in data
                    where n > 3
                    select n;
    
    
    
            List<int> b = a.ToList();
            for (int i = 0; i < b.Count(); i++)
            {
                b\[i\] = 10\*b\[i\];
                Console.WriteLine(b\[i\]);
            }
    
            for (int i = 0; i < data.Count(); i++)
                Console.WriteLine(data\[i\]);
    

    Data still contains 0,1,2,3,4,5 instead of 0,1,2,3,40,50,which is what i want. Please help, many thanks.

    G D 2 Replies Last reply
    0
    • U uusheikh

      I have a simple question, but i don't know how to do it. I have a collection of data, and would like to query it using LINQ. Then, i want to modify the resultant collection, which updates the original collection For example i have the following collection int[] data = { 0, 1, 2, 3, 4, 5 }; I want to select all data larger than 3 (thus 4 and 5), and then modify them by multiplying them with 10. My final collection should look like this. (This is a simple example, in reality, i will be using a complex object) data = {0, 1, 2, 3, 40 , 50}; I tried this; but doesn't update the original collection. Does ToList() return a copy of the collection? How do i go about doing this?

      int[] data = { 0, 1, 2, 3, 4, 5 };

              var a = from n in data
                      where n > 3
                      select n;
      
      
      
              List<int> b = a.ToList();
              for (int i = 0; i < b.Count(); i++)
              {
                  b\[i\] = 10\*b\[i\];
                  Console.WriteLine(b\[i\]);
              }
      
              for (int i = 0; i < data.Count(); i++)
                  Console.WriteLine(data\[i\]);
      

      Data still contains 0,1,2,3,4,5 instead of 0,1,2,3,40,50,which is what i want. Please help, many thanks.

      G Offline
      G Offline
      Gideon Engelberth
      wrote on last edited by
      #2

      LINQ is not designed for that sort of thing. LINQ is designed to be "functional" in that it aims to provide methods that do not affect the state from which they are queried. You will need to do a for loop yourself to do this.

      1 Reply Last reply
      0
      • U uusheikh

        I have a simple question, but i don't know how to do it. I have a collection of data, and would like to query it using LINQ. Then, i want to modify the resultant collection, which updates the original collection For example i have the following collection int[] data = { 0, 1, 2, 3, 4, 5 }; I want to select all data larger than 3 (thus 4 and 5), and then modify them by multiplying them with 10. My final collection should look like this. (This is a simple example, in reality, i will be using a complex object) data = {0, 1, 2, 3, 40 , 50}; I tried this; but doesn't update the original collection. Does ToList() return a copy of the collection? How do i go about doing this?

        int[] data = { 0, 1, 2, 3, 4, 5 };

                var a = from n in data
                        where n > 3
                        select n;
        
        
        
                List<int> b = a.ToList();
                for (int i = 0; i < b.Count(); i++)
                {
                    b\[i\] = 10\*b\[i\];
                    Console.WriteLine(b\[i\]);
                }
        
                for (int i = 0; i < data.Count(); i++)
                    Console.WriteLine(data\[i\]);
        

        Data still contains 0,1,2,3,4,5 instead of 0,1,2,3,40,50,which is what i want. Please help, many thanks.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        On top of what Gideon said, you don't have any code that modified the array at all. You mode changes to your "b" list (which is a seperate object from the "data" array), but then you displayed the original data list. Oh, and BTW. Array's in .NET are immutable. You cannot change the size of an array once it's created. Your LINQ query returned a List object containing 2 integers, 4 and 5. It does NOT return the source array you are querying. Then you multiplied the two integers in that List by 10 and over-wrote the values in the List with the new values. You didn't touch the original Array of numbers at all, but then you displayed them.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008
        But no longer in 2009...

        U 1 Reply Last reply
        0
        • D Dave Kreskowiak

          On top of what Gideon said, you don't have any code that modified the array at all. You mode changes to your "b" list (which is a seperate object from the "data" array), but then you displayed the original data list. Oh, and BTW. Array's in .NET are immutable. You cannot change the size of an array once it's created. Your LINQ query returned a List object containing 2 integers, 4 and 5. It does NOT return the source array you are querying. Then you multiplied the two integers in that List by 10 and over-wrote the values in the List with the new values. You didn't touch the original Array of numbers at all, but then you displayed them.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008
          But no longer in 2009...

          U Offline
          U Offline
          uusheikh
          wrote on last edited by
          #4

          Thanks, now i understand.

          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