var result = users
.Where(x => x.IsActive)
.GroupBy(x => x.City)
.Select(x => new MyModel
{
Name = x.First().Name,
Age = x.First(),
CreatingDate = x.First().CreationDate,
HairColor = x.First().HairColor
})
.ToArray();
How many times will the x.First() method be called for each group? Does this mean that the method will be called 4 times for each group. Or is there an optimization that fetches an element once using a x.First() method, then saves it to a variable and uses the variable to retrieve the fields?
CodePudding user response:
How many times will the x.First() method be called for each group?
4
Does this mean that the method will be called 4 times for each group
Yes
Or is there an optimization that fetches an element once using a x.First() method, then saves it to a variable and uses the variable to retrieve the fields?
No, you would have to do that yourself. For this to be done the jitter would need to prove that the result are the same for each call. While this would be possible, it would be complicated and expensive to do, so the jitter most likely will not bother.
However, .First()
should be a constant time function, so the overhead is much less than for example .Last()
. But if you are more concerned about performance than readability you should probably avoid linq altogether, since the abstraction level Linq provides does have some overhead.
And if you are concerned about performance, measure! We might make educated guesses about performance, but the only way to be sure is to benchmark the code.
CodePudding user response:
JonasH gave an answer to your question, but if you want the answer to the question "how can I avoid multiple First
calls" then check this code out:
var result = users
.Where(x => x.IsActive)
.GroupBy(x => x.City)
.Select(x => new { Key = x.Key, First = x.First() })
.Select(x => new MyModel
{
Name = x.First.Name,
Age = x.First.Age, // I assume there's a typo here in your code
CreatingDate = x.First.CreationDate,
HairColor = x.First.HairColor
})
.ToArray();