That's not a safe thing to do: the default value for any reference type (which I assume MyRole is) is
null
- so LastOrDefault will return
null
if there are no matching values.
As a result,
.LastOrDefault().AccessLevelId;
will cause your program to crash with an "null reference exception"
In terms of execution, the two are probably equivalent: Where will return an IEnumerable which the parameterless version of LastOrDefault will extract or the last value, if any, and the parameterised version with do the equivalent of the Where internally. If may be slightly more efficient to use the parameterised version, but I suspect that would depend on the exact make up of the input IEnumerable. It'd be a PITA to do performance testing on though, given that they are deferred execution queries anyway...
I'd go with the first version - I find it easier to read! :laugh: