You shouldn’t use ForEach
in that way. Read Lippert’s “foreach” vs “ForEach”
If you want to be cruel with yourself (and the world), at least don’t create useless List
employees.All(p => { collection.AddRange(p.Departments); p.Departments.All(u => { u.SomeProperty = null; return true; } ); return true; });
Note that the result of the All
expression is a bool
value that we are discarding (we are using it only because it “cycles” all the elements)
I’ll repeat. You shouldn’t use ForEach
to change objects. LINQ should be used in a “functional” way (you can create new objects but you can’t change old objects nor you can create side-effects). And what you are writing is creating so many useless List
only to gain two lines of code…
Related Posts:
- Group by in LINQ
- Join/Where with LINQ and Lambda
- LINQ’s Distinct() on a particular property
- Sequence contains no elements?
- LEFT OUTER JOIN in LINQ
- Validation failed for one or more entities. See ‘EntityValidationErrors’ property for more details
- Extension methods must be defined in a non-generic static class
- Proper Linq where clauses
- Validation failed for one or more entities. See ‘EntityValidationErrors’ property for more details
- Extension methods must be defined in a non-generic static class
- Select distinct using linq
- Convert Linq Query Result to Dictionary
- EF 5 Enable-Migrations : No context type was found in the assembly
- Get single value from dictionary by key
- The entity type ApplicationUser is not part of the model for the current context
- IEnumerable vs List – What to Use? How do they work?
- Sequence contains no matching element
- The entity type
is not part of the model for the current context - EntityType has no key defined error
- EF CodeFirst: Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong
- There is already an open DataReader associated with this Command which must be closed first
- Conversion of a datetime2 data type to a datetime data type results out-of-range value
- MSSQL Error ‘The underlying provider failed on Open’
- entity object cannot be referenced by multiple instances of IEntityChangeTracker. while adding related objects to entity in Entity Framework 4.1
- Cannot implicitly convert type ‘System.Collections.Generic.List< >‘ to ‘System.Collections.Generic.IList< >‘
- Linq select objects in list where exists IN (A,B,C)
- How do I concatenate two arrays in C#?
- Unable to determine the principal end of an association between the types
- Convert SQL to LINQ Query
- The provider did not return a ProviderManifestToken string Entity Framework
- Sequence contains more than one element
- Quickest way to compare two generic lists for differences
- LINQ query on a DataTable
- Using .Select and .Where in a single LINQ statement
- Linq: GroupBy, Sum and Count
- Convert HTML to PDF in .NET
- How to show a loading gif while an APi is being called in xamarin android?
- ImageMagick creating blank transparent square(s) according to width
- .Net 4.8 Support for Windows 10 ends in 26 days?
- How do I generate a random int number?
- Why has the int32 type a maximum value of 2³¹ − 1? [duplicate]
- What is a NullReferenceException, and how do I fix it?
- How do I get a random YouTube video with the YouTube API?
- An existing connection was forcibly closed by the remote host
- C# HttpClient An existing connection was forcibly closed by the remote host
- How can I convert String to Int?
- What is a NullReferenceException, and how do I fix it?
- What is a PDB file?
- What is the { get; set; } syntax in C#?
- Unexplained crashes related to ntdll.dll
- I am getting Failed to load resource: net::ERR_BLOCKED_BY_CLIENT with Google chrome
- How can I parse JSON with C#?
- Can anyone explain IEnumerable and IEnumerator to me? [closed]
- What is parsing?
- What is IP address ‘::1’?
- CS0120: An object reference is required for the nonstatic field, method, or property ‘foo’
- IOException: The process cannot access the file ‘file path’ because it is being used by another process
- How to find child of a GameObject or the script attached to child GameObject via script
- Process cannot access the file because it is being used by another proess
- Getting mouse position in unity
- Error : Index was outside the bounds of the array.
- How to fix “HTTP Error 400. The size of the request headers is too long” error from azure cloud services?
- C# error: “An object reference is required for the non-static field, method, or property”
- No connection could be made because the target machine actively refused it?
- How to convert JSON to XML or XML to JSON?
- Virustotal Trapmine suspicious.low.ml.score
- What is the difference between declarative and imperative paradigm in programming?
- How to fix No connection could be made because the target machine actively refused it 127.0.0.1:64527
- Could not load file or assembly or one of its dependencies
- What does a lock statement do under the hood?
- How to make the script wait/sleep in a simple way in unity
- What is the yield keyword used for in C#?
- Error : Could not load file or assembly or one of it’s dependencies
- Convert int to string?
- Converting string to byte array in C#
- Could not load file or assembly or one of its dependencies
- All possible array initialization syntaxes
- Adding values to a C# array
- C# compiler error: “not all code paths return a value”
- What is C# equivalent of
- How to use SQLite in Visual Studio 2017?
- Algorithms in O(n^2) vs O(n) [duplicate]
- Unity OnTriggerEnter2D not registering
- What are the benefits to marking a field as `readonly` in C#?
- Priority queue in .Net
- How to Sort a List
by a property in the object - What is the difference between int, Int16, Int32 and Int64?
- How to resolve “Input string was not in a correct format.” error?
- failed to load resource: the server response with a status 500 (internal server error)
- Adding values to a C# array
- C# compiler error: “not all code paths return a value”
- How can I add ” character to a multi line string declaration in C#?
- C# equivalent of C++ map
- Input string was not in a correct format
- The request was aborted: Could not create SSL/TLS secure channel
- How to convert byte array to string [duplicate]
- C# Java HashMap equivalent
- The remote certificate is invalid according to the validation procedure [duplicate]
- The page was not displayed because the request entity is too large on IIS
- The page was not displayed because the request entity is too large on IIS