Is it possible to add if
-statement inside LINQ ForEach
call?
sequence.Where(x => x.Name.ToString().Equals("Apple")) .ToList() .ForEach( /* If statement here */ );
Most of the times, LINQ will be a bit slower because it introduces overhead. Do not use LINQ if you care much about performance. Use LINQ because you want shorter better readable and maintainable code. So your experience is that LINQ is faster and makes code harder to read and to maintain?
At 3M elements, the LINQ implementation takes about 4 milliseconds longer than for/foreach.
The forloop is faster than the foreach loop if the array must only be accessed once per iteration.
you can do the following...
List.Where(x => x.Name.ToString().Equals("Apple").ToList() .ForEach( x => { if(x.Name == ""){}} );
Yes, if-statement is commonly used inside the ForEach as below:
sequence.Where(x => x.Name.ToString().Equals("Apple")) .ToList() .ForEach( x => { if(someCondition) { // Do some stuff here. } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With