I don't know if this is even possible, but how can I access an object property that has be set within an initializer so that I can use it to set another property within the same initializer?
Here's what I'm trying to do:
var usersWithCount =
users
.AsEnumerable()
.Select(
u =>
new User()
{
UserId = u.UserId,
UserName = u.UserName,
Email = u.Email,
RelatedId = u.RelatedId,
ReviewCount = u.Reviews.Count(r => !r.Deleted && r.Approved),
HelpfulYesCount = u.Reviews.Where(r => !r.Deleted && r.Approved).Sum(r => r.HelpfulYes),
HelpfulNoCount = u.Reviews.Where(r => !r.Deleted && r.Approved).Sum(r => r.HelpfulNo),
TotalPoints = ReviewCount + HelpfulYesCount - HelpfulNoCount,
DateCreated = u.DateCreated
})
.OrderByDescending(user => user.TotalPoints);
The part that doesn't work is "TotalPoints = ReviewCount + HelpfulYesCount - HelpfulNoCount". I'd rather avoid using "u.Reviews.Count(r => !r.Deleted && r.Approved)" again and I'd don't want to have to loop through the results to add those values together to set TotalPoints.
How can I reference those properties within the initializer that were set above the TotalPoints property? Is there some way I can set them equal to variables and reference them where they are set and where I'm trying to add them? Am I approaching this situation the completely wrong way?