This question already has answers here:
Why is there no ForEach extension method on IEnumerable?
(21 answers)
Closed 2 years ago.
Theres a Do method in System.Reactive to a execute a mutator for each item in the sequence.
Is there any equivalent method for IEnumerable either in standard library or third parties like morelinq?
LINQ query operators are generally meant to be free from side-effects. You could just use a foreach loop to "do" your thing.
Or do it in a Select or Where method if you don't care about side effects:
enumerable.Select(x => { /*do something here */ return x; })
For a List<T>, there is a ForEach extension method that can be used to execute an Action<T> for each element.
Related
This question already has answers here:
Why would you use Expression<Func<T>> rather than Func<T>?
(12 answers)
Closed 3 years ago.
I am starting to see methods like the one below used more and more, but it's a concept that I don't fully understand.
public virtual Task<List<T>> GetAsync(Expression<Func<T, bool>> exp)
{
using (var conn = _factory.OpenDbConnection())
{
return conn.SelectAsync(exp);
}
}
Can someone help me translate the method parameter there that is an Expression? Like explain it as how it differs from a standard instance parameter?
These are called Expression Trees (https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/expression-trees/).
Basically it's a lamdba that can be translated to another platform, from the looks of it some kind of database in your case. This function would be translated (by a library) to SQL and then executed in the database.
Within the code of your program you would generally only need lambdas (Func<>), but in some cases you need an Expression Tree. Besides your example, sometimes you need a dynamically constructed function which can be done using these.
General information on lambdas can be found here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions
This question already has answers here:
FindAll vs Where extension-method
(5 answers)
Slow LINQ query for .ToArray()
(3 answers)
C# FindAll VS Where Speed
(5 answers)
Closed 3 years ago.
I have an array of structs and I need to locate all the elements that share a certain condition.
Currently I use:
Array.FindAll(someArray, x => x.eg == "*Perfomance Test*")
But after some googling I noticed that there is a much faster way by using the LINQ Where-method.
someArray.Where(x => x.eg == "*Perfomance Test*")
I did some testing and the results are pretty impressive:
FindAll: 00:00:03.06
Where: 00:00:00.20
The problem is that Where returns IEnumerable. And I call this method that returns all these certain elements from the array within the for loop. Which is the type of loop I need to use. I used the .ToArray() method, but that made it much worse so that FindAll() is the faster approach.
Because of that it seems to me that if I need to get an array of elements, Where is much slower option than FindAll. But maybe I'm missing something.
Are there any better, faster options?
This question already has answers here:
Array versus List<T>: When to use which?
(16 answers)
Closed 3 years ago.
If some method requires an ICollection<T> as an argument, but I have only an IEnumerable<T> available: Is it better to convert this IEnumerable<T> to a IList<T> or is it better to convert it to an array T[] or shall I convert it to something else or is there no difference at all?
The IEnumerable<T> is only required for this method call, so no further read/write/extend is required.
If you're not going to be accessing the elements, you should use ToList() as explained why here.
If you'll be iterating through the elements, then it'd be better performance-wise to do it via an index (meaning casting them to an array), so that you can use a for loop, instead of a foreach loop.
for is faster than your typical foreach due to it using an index to access each element.
This question already has answers here:
foreach vs someList.ForEach(){}
(13 answers)
Closed 9 years ago.
Is there any performance difference between these two statements?
IEnumerable<T>.ToList().ForEach(x => Console.WriteLine(x));
and
foreach (var obj in IEnumerable<T>)
Console.WriteLine(obj)
In the first example, you will
Create a list by enumerating the source
Enumerate through the new list and call Console.WriteLine for each element.
In the second example, you will
Enumerate through the source and call Console.WriteLine for each element.
There are two performance penalities to the first over the second:
The creation of the new List object
The double enumeration: over the original source, and then the list
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is there not a ForEach extension method on the IEnumerable interface?
Why ForEach extension method is not available in ObservableCollections class while it is available in List ?
As JJohn says, it's by design. Read this post by Eric Lippert for more information. A standard foreach loop works just as well and is generally more readable.
ForEach is available only in List<T> and not in any other collection. It is just design decision.
Foreach can be used only on those objects which are of classes that implements IEnumerable.
IEnumerable is an interface that defines one method GetEnumerator which returns an IEnumerator interface, this in turn allows readonly access to a collection. A collection that implements IEnumerable can be used with a foreach statement.