score:8
first off, this is not a good idea. see below for arguments against it.
it doesn't work. the property value is not changed. why?
it doesn't work because select()
doesn't actually iterate through the collection until you enumerate it's results, and it requires an expression that evaluates to a value.
if you make your expression return a value, and add something that will fully evaluate the query, such as tolist()
, to the end, then it will "work", ie:
myobjectcollection.select(myobject => { myobject.myproperty = newvalue; return myobject;}).tolist();
that being said, tolist()
has some disadvantages - mainly, it's doing a lot of extra work (to create a list<t>
) that's not needed, which adds a large cost. avoiding it would require enumerating the collection:
foreach(var obj in myobjectcollection.select(myobject => { myobject.myproperty = newvalue; return myobject; }))
{ }
again, i wouldn't recommend this. at this point, the select
option is far uglier, more typing, etc. it's also a violation of the expectations involved with linq - linq is about querying, which suggests there shouldn't be side effects when using linq, and the entire purpose here is to create side effects.
but then, at this point, you're better off (with less typing) doing it the "clear" way:
foreach (var obj in myobjectcollection)
{
obj.myproperty = newvalue;
}
this is shorter, very clear in its intent, and very clean.
note that you can add a foreach<t>
extension method which performs an action on each object, but i would still recommend avoiding that. eric lippert wrote a great blog post about the subject that is worth a read: "foreach" vs "foreach".
score:0
as sircodesalot mentioned, you can't use linq to do something like that. remember that linq is a querying language, which means all you can do is query it. doing changes must be done in other logic.
what you could do, if you don't want to do it the first way and if your collection is a list already (but not an ienumerable) you can use the foreach extension method in linq to do what you're asking.
one other point i should mention is that the select method does a projection of some specific information to return to an ienumerable. so, if you wanted to grab only a specific property from a collection you would use that. that's all it does.
Source: stackoverflow.com
Related Query
- Using LINQ to change values in collection
- Getting a collection of index values using a LINQ query
- How can I iterate over a collection and change values with LINQ extension methods?
- Change values of datatable column conditionally using Linq
- Retriving Sum of values under object collection using linq c#
- How to change values of few properties from one list to another using LINQ C#:
- How to replace values smaller than zero with zero in a collection using LINQ
- C# How to change Xml node values using Linq
- Change items bool property in a collection if they match/not match the index containing in other list using LINQ
- Update all objects in a collection using LINQ
- Convert string[] to int[] in one line of code using LINQ
- How to select values within a provided index range from a List using LINQ
- Select All distinct values in a column using LINQ
- How to select multiple values from a Dictionary using Linq as simple as possible
- How to remove duplicates from collection using IEqualityComparer, LinQ Distinct
- Get indexes of all matching values from list using Linq
- Convert dictionary values to list using linq
- Using LINQ to Objects to find items in one collection that do not match another
- Collection to string using linq
- Linq to update a collection with values from another collection?
- Query and updating a property in a collection using LINQ
- Select distinct values from a list using LINQ in C#
- Using Linq to find the element after a specified element in a collection
- Using Linq to do a Contains with multiple values
- Get new indices of items in a collection after sorting using LINQ
- Using Linq to run a method on a collection of objects?
- Change the property of objects in a List using LINQ
- linq how to select a parent with a child collection that contains one or many of an array (or list) of values
- Using LINQ to create an IEnumerable<> of delta values
- Count values in Dictionary using LINQ and LINQ extensions
More Query from same tag
- Skip is not currently supported by the Data Service tool kit exception
- How to extract a specific value from the linq query?
- How can I query my join table (many-to-many) using Linq and EF6 (get a users roles)
- How can I use the MYSQL BIT_COUNT function through nhibernate/fluent nHibernate?
- Strange LINQ To Entities Exception
- Inserting "raw" XML string into a XElement
- Not getting the difference between two list by using linq
- EditorFor Field for List object that maps to a different model?
- How do I nest a call to a method within a call to a method using Expression.Call
- How to calculate an ETA for a LINQ pipeline to complete?
- C# compiler (csc.exe) memory overflow compiling nested types and Linq
- LINQ Row Index Retrieval Cast Requirement
- LINQ return type and use in the other function
- LINQ for 3 tables - new to LINQ
- Some confusion with conversion of FROM clauses into a call to SelectMany
- Select first word of list of strings and concat last char of the rest of items on list in linq
- Compile expression that requires a parameter
- Linq to SQL: WHERE IN statement
- Delete function skipped in asp.net
- How can "group by" be done using C# Linq to achieve it
- LINQ to Subsonic Left Outer Join
- Linq FirstOrDefault ordering when OrderBy or OrderByDescending are not supplied?
- Entity Framework/LINQ: Selecting columns from multiple tables?
- How to easly convert linq result to Business Object Collection <T>
- dynamic, linq and Select()
- XDocument Traversal
- linq on a DataTable giver compilation error
- Group by with Linq Dynamic
- Calling static generic LINQ extension method in PowerShell
- Iterate through Cart GroupBy and Sum