I
I
Ivan Karabadzhak2012-11-26 14:46:13
.NET
Ivan Karabadzhak, 2012-11-26 14:46:13

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;
    }


Code - not a required parameter, Year - the year the classes started, Month - the month the classes started, TotalHours - the number of hours of classes in each month.

Here is a list of clients.

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));


And now the task itself. It is necessary for each “YEAR-MONTH” pair, for each client, to calculate how many hours he has been studying since this date (or 0 if he began to study later), then sum these values ​​​​for the current date and at the output get the list “YEAR - MONTH - TIME ".

That is, take a year and a month in turn, then for each client count how much he has been doing since that moment in time (a year-month pair), and sum up all the values ​​for this pair. Then take the next pair and do the same.

To be honest, without linq and lambda expressions I will do it quickly, but with them how - I have no idea. Please help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Piskov, 2012-11-26
@Piskov

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
  };

I
Ivan Karabadzhak, 2012-11-27
@Jakeroid

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 question

Ask a Question

731 491 924 answers to any question