Anonymous types are immutable. If the type is not anonymous, you can dump the collection to a List and modify that: (from e in empList) .ToList() . ForEach(e => { e.
Salary = 999; }) Notice I'm using block syntax in the lambda.
Anonymous types are immutable. If the type is not anonymous, you can dump the collection to a List and modify that: (from e in empList) .ToList() . ForEach(e => { e.
Salary = 999; }) ; Notice I'm using block syntax in the lambda.
1 However, this will be slower than a foreach loop. (Because of the ToList call) – SLaks? Apr 1 '10 at 14:46 And, all things being equal, a lambda expression will be slower (to invoke, though not to execute) than normal procedural code.
Lambdas are a good idea when they make the code more readable or concise, but I don't see how this is particularly more readable or concise than a simple foreach statement. – Adam Robinson Apr 1 '10 at 14:53 I use LINQPad for a lot of misc tasks involving hundreds of thousands of records. In my experience the ToList() call is negligible compared to other bottlenecks.
– ifwdev Apr 1 '10 at 14:53 .AsParallel() . ForAll(x => { e. Salary = 999; }) ; Is NOT slower than a foreach loop in .
NET 4 btw – ifwdev Apr 1 '10 at 14:55 @ifwdev: That's certainly not true as an absolute statement. Execute that on a single-core processor and you're needlessly splitting the list into multiple threads to no advantage. – Adam Robinson Apr 1 '10 at 14:58.
1 This is correct, the foreach loop is designed to operated over an Enumerable... you can even use a for loop LINQ was designed to QUERY collections, not iterate over them – masenkablast Apr 1 '10 at 14:44 1 He can't modify the objects created in his example. Anonymous types are immutable. – Kevin Babcock Apr 1 '10 at 14:50 1 I don't think he meant to use an anonymous type at all – masenkablast Apr 1 '10 at 14:50.
That depends on what you're asking. If you're asking: Can I use a LINQ statement to update data on the elements of a collection? Then the answer is simply no; LINQ is a query language, not a data modification language.
Just use a foreach loop on the original collection as you would have done prior to . NET 3.5/C# 3.0. Nothing to see here.
If you're asking Can I update the values on the type I created in the query in my question? , then the answer is "sortof". The type you're creating by using new { ... } is an anonymous type, which makes it immutable (read only).
There's no clean way to do what you're after, but you could do something like: var query = from e in empList select new { id = e. Id, salary = e. Salary / 10.0, record = e } Doing this will give you a reference to the original object via the record property, which you can change values on.
Realize, though, that the values on your anonymous type (id and salary, in this case) will not reflect any changes made to the referenced object in record. Foreach(var element in empList) { element.record. Salary = 100.0; } This will update the salary property on the original item, but element.
Salary will still reflect the original value that was calculated in the query, not the new salary value / 10.0.
Sure, that will work just fine. Note, however, that the object you are create, while looking like and having the same properties as an Emp, will be a distinct object type. To elaborate, if you were to say: var query =from e in empList select e; You are creating a collection of Emp objects.
When you say: var query =from e in empList select new {id=e. Id,salary=e. Salary * (10/100) }; You are creating a collection of anonomous objects which have the same form as Emp objects.
You can assign values which may be different from the original empList value, while you are creating it, but once that statement is complete, thosee values cannot be changed again. And nothing here will affect the values in empList.
No, it won't; anonymous types are immutable. – Adam Robinson Apr 1 '10 at 14:48.
Then the answer is "sortof". The type you're creating by using new { ... } is an anonymous type, which makes it immutable (read only). Doing this will give you a reference to the original object via the record property, which you can change values on.
Realize, though, that the values on your anonymous type (id and salary, in this case) will not reflect any changes made to the referenced object in record. This will update the salary property on the original item, but element. Salary will still reflect the original value that was calculated in the query, not the new salary value / 10.0.
Anonymous types are immutable. If the type is not anonymous, you can dump the collection to a List and modify that.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.