score:6
the foreach
is preferred for what you're trying to do. you're iterating over a sequence of elements and modifying the elements. that's what foreach
is for.
linq is used to take one sequence of elements and generate a new sequence based on some criteria/transformation. not what you're after.
score:0
your first foreach loop is pretty clear and it works, so why need to replace it with linq, it wont be more clear or faster.
otherwise i dont think it will be simpler than what you say above
chart1.series.tolist().foreach( series => series.enabled = false);
score:0
as to the specific reason your select didn't work, bear in mind that select returns an ienumerable<t>
, which is an object that knows how to enumerate. in other words, you have a reference to something that can enumerate, but that hasn't yet been enumerated. so if you take this:
var series = chart1.series.select( s => s.enabled = false);
foreach (var s in series) {}
you'll get the effect you intended, because now you're enumerating the series and therefore calling the select delegate. this is rather non-intuitive, of course, which is why select is typically not used in this way (otherwise every time you enumerate, you'll have side effects and enumerating more than once would apply the side effects again.)
score:1
simple and one line :)
foreach (var series in chart1.series) { series.enabled = false; }
Source: stackoverflow.com
Related Query
- Simple C# foreach to LINQ question
- LINQ Source Code Available
- Simple LINQ question in C#
- C# Linq query help removing foreach loops creating cleaner code
- Convert simple ForEach into a Linq statement
- Linq Efficiency question - foreach vs aggregates
- convert foreach loop to linq code
- creating Linq to sqlite dbml from DbLinq source code
- Simple LINQ question - how to iterate through a group?
- C# LINQ question about foreach
- Grouping in LINQ - simple question
- Converting foreach loop to LINQ query breaks code
- My code is very inefficient for this simple Linq usage
- LINQ A simple question
- Simple Linq Dynamic Query question
- source code for LINQ 101 samples
- C# LINQ Find List Inside Another List, Better way to code this than a foreach loop
- Simple Linq to XML question
- Simple question regarding LINQ
- Convert the code from Foreach loop to Linq
- Simple LINQ question about Any()
- Cleaning up a simple foreach with linq
- Is there a way I can do a foreach that contains code in a LINQ expression?
- Simple linq question: How to filter a source afterwards?
- c# Linq or code to extract groups from a single list of source data
- I want to convert the following code in Foreach to Linq
- How to convert the following foreach loop to linq code format?
- LINQ equivalent of foreach for IEnumerable<T>
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
More Query from same tag
- LINQ to Entities query in "dot notation"
- Condition in Linq Expression
- The LINQ expression could not be translated using Shadow Properties in MVC Core
- Split a long string to a customized string
- Using linq with PropertyValueCollection
- using linq to sql to find common data
- IEnumerable<T, int>, Arity and Generic Type Definitions
- Linq, filter and retrieval using complex class structure
- Select users with similar name, but different password
- LINQ: "Types in Union or Concat are constructed incompatibly."
- Top Highscore LINQ to SQL
- Amend list using linq via second list
- NHibernate QueryOver can't query lazily loaded objects
- EF LEFT OUTER JOIN instead of INNER JOIN in one to many relationships
- Materialising "Friendly" Entities from a disconnected EF5 Context
- Selecting Closest Prior Year Cash From DB
- ObjectDisposedException despite *Include* being used
- Why does c# Linq use reference instead of value in List containing List
- How can I reuse my explicit column selection?
- Join two tables, select column based on value in first table
- Looping through fields to get sum
- LINQ-to-SQL class doesn't implement INotifyPropertyChanging & INotifyPropertyChanged if pulling from local database
- Converting SQL to LINQ query
- Perform .Select() on IQueryable with List of Func<T, string> delegates
- Get maximums of list using Linq?
- How to get the top 3 elements in an int array using LINQ?
- How to use lambda expression with joins
- Expression.Coalesce in Linq to Entities
- Manipulating data in a collection with LINQ
- FirstOrDefault() returns first letter of the string