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