Answer the question
In order to leave comments, you need to log in
Nested LINQ query with lambda expression
There is a very strange task, I just can not figure it out. A prerequisite is the use of a lambda expression to solve the problem.
The task itself. There is a fitness center. There is a list of clients of this class.
class Client
{
public Client(int code, int year, int month, int totalHours)
{
Code = code;
Year = year;
Month = month;
TotalHours = totalHours;
}
public override string ToString()
{
return Code + " " + Year + " " + Month + " " + TotalHours;
}
public int Code;
public int Year;
public int Month;
public int TotalHours;
}
List<Client> clients = new List<Client>();
clients.Add(new Client(1, 2002, 10, 2));
clients.Add(new Client(2, 2008, 1, 15));
clients.Add(new Client(3, 2010, 12, 1));
clients.Add(new Client(4, 2009, 3, 25));
clients.Add(new Client(5, 2001, 7, 20));
clients.Add(new Client(6, 2000, 7, 1));
clients.Add(new Client(7, 2007, 9, 3));
Answer the question
In order to leave comments, you need to log in
And what is the strangeness? The code is exactly the same as without the link, if I understand your task correctly
var results = from c in clients
let elapsedMonths = (DateTime.Now.Year * 12 + DateTime.Now.Month) - (c.Year * 12 + c.Month)
select new
{
Year = c.Year,
Month = c.Month,
Elapsed = elapsedMonths > 0 ? elapsedMonths * c.TotalHours : 0
};
Unfortunately, you misunderstood me. Or maybe I explained badly. The solution to the problem is the following expression.
var result6 = clients.Select(i => new { Year = i.Year, Month = i.Month, TotalTime = clients.Select(j => j.Year < i.Year ? 0 : ((j.Year - i.Year) * 12 + (j.Month - i.Month)) * j.TotalHours).Sum() } ).ToList();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question