C
C#11mo ago
Dyad

How to do this with LINQ?

``` var values = new []{ 1, 2, 3, 4, 5} int winner; for(int i = 0; i < values.Count; i++) { var value = values[i]; var r = value * 2; if(r > 7) { winner = r; break; } }
41 Replies
Moods
Moods11mo ago
What does value end up doing
Jimmacle
Jimmacle11mo ago
var winner = values.FirstOrDefault(x => x * 2 > 7)?
Dyad
DyadOP11mo ago
I know I can do values.Select(v => v * 2).Where(r => r > 7).First() but I want to do it efficiently
mindhardt
mindhardt11mo ago
More like LastOrDefault
Dyad
DyadOP11mo ago
I don't want to do the computation if something wins early
Jimmacle
Jimmacle11mo ago
why last? it breaks at the first winner
Moods
Moods11mo ago
they’re comparing indices not values are they not?
mindhardt
mindhardt11mo ago
Ah true
Jimmacle
Jimmacle11mo ago
yeah i'm assuming there's a typo in this part
var value = values[i];
var r = i * 2;
var value = values[i];
var r = i * 2;
mindhardt
mindhardt11mo ago
This one is pretty much efficient, linq chain is executed as many times as needed
Jimmacle
Jimmacle11mo ago
yeah the only efficiency to be gained is typing less by putting the whole expression in FirstOrDefault (default because there may not be any winner)
Dyad
DyadOP11mo ago
in my case I have a more complex algorithm I don't want to run if there is an early winner
Jimmacle
Jimmacle11mo ago
and performance wise, a plain loop is probably faster than linq anyway
mindhardt
mindhardt11mo ago
First(OrDefault) runs till it either finds a winner or collection ends Their difference is the behavior in case of no winners
Dyad
DyadOP11mo ago
the problem with first or default is it doesn't remember the computation
Moods
Moods11mo ago
Tempted to agree but LINQ’s probably been optimized to hell they could be virtually the same
mindhardt
mindhardt11mo ago
Wym
Dyad
DyadOP11mo ago
values.FirstOrDefault(v => v x 2 > 7) x 2
mindhardt
mindhardt11mo ago
This, or
Dyad
DyadOP11mo ago
I would have to do the math again on the winning value cause it doesn't remember the computation that happened in the lamba
mindhardt
mindhardt11mo ago
values.Select(x => x * 2).FirstOrDefault(x => x > 7)
Jimmacle
Jimmacle11mo ago
^ but then you don't know who the winner is
Dyad
DyadOP11mo ago
I want to terminate early
mindhardt
mindhardt11mo ago
Do they need to?
Dyad
DyadOP11mo ago
so I can't just select on all values
Jimmacle
Jimmacle11mo ago
select doesn't enumerate the whole list
mindhardt
mindhardt11mo ago
I guess you don't understand linq chains
Jimmacle
Jimmacle11mo ago
linq is evaluated lazily the computation is only done when the next value is needed, like when you're materializing the result with First
mindhardt
mindhardt11mo ago
It doesn't equal to two loops: one for .Select and one for FirstOrDefault It equals to one loop which firstly transforms and then checks and may or may not return
Jimmacle
Jimmacle11mo ago
how many values are you computing this on?
Moods
Moods11mo ago
All Select does is create an object to hold the lambda and source array/list (very watered down explanation)
Jimmacle
Jimmacle11mo ago
because unless it's millions it doesn't even matter a single multiplication is very cheap
Dyad
DyadOP11mo ago
oh it runs both lambdas on the first value and will only proceed to the next if the 2nd lambda condition is not met?
Jimmacle
Jimmacle11mo ago
essentially
mindhardt
mindhardt11mo ago
More or less
Dyad
DyadOP11mo ago
oh, you learn something new everyday thanks guys 🙂
mindhardt
mindhardt11mo ago
$close
MODiX
MODiX11mo ago
Use the /close command to mark a forum thread as answered
Moods
Moods11mo ago
Yeah both lambdas get evaluated in the FirstOrDefault call
Jimmacle
Jimmacle11mo ago
i will reiterate that if performance is the goal and you're concerned about a single multiplication taking too long, you shouldn't be using linq
mindhardt
mindhardt11mo ago
But linq is so comfy catpog
Want results from more Discord servers?
Add your server