Попробуйте запрос ниже:
class Program
{
static void Main(string[] args)
{
var results = (from o in MyList
group o by new { o.Organization } into g
select new
{
Org_Id = g.Key.Organization,
Year = g.Select(x => x.Year)
.Max(),
Month = g.Where(x => x.Year == g.Select(y => y.Year).Max())
.Select(z => z.Month)
.Max(),
Sum = g.Where(x => x.Month == g.Where(y => y.Year == g.Select(z => z.Year).Max())
.Select(y => y.Month)
.Max())
.Select(z => z.Value)
.Sum()
}).ToList();
results.ForEach(x => Console.WriteLine($"Org_Id: {x.Org_Id} \t Year: {x.Year} \t Month: {x.Month} \t Sum: {x.Sum}"));
Console.ReadLine();
}
}
Что мы сделали в запросе:
1) Группировка по Organization
2) Org_Id
: в качестве ключа вашей группы
3) Year
: выберите Max
год из группы.
4) Month
: выберите Max
месяца, выбрав Max
года из группы.
5) Sum
: сумма значения путем выбора Max
месяца Max
года из группы.
Выход:
Sql для вышеуказанного запроса:
SELECT [t1].[Organization] AS [Org_Id], (
SELECT MAX([t2].[Year])
FROM [Org] AS [t2]
WHERE (([t1].[Organization] IS NULL) AND ([t2].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t2].[Organization] IS NOT NULL) AND ((([t1].[Organization] IS NULL) AND ([t2].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t2].[Organization] IS NOT NULL) AND ([t1].[Organization] = [t2].[Organization]))))
) AS [Year], (
SELECT MAX([t3].[Month])
FROM [Org] AS [t3]
WHERE ([t3].[Year] = ((
SELECT MAX([t4].[Year])
FROM [Org] AS [t4]
WHERE (([t1].[Organization] IS NULL) AND ([t4].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t4].[Organization] IS NOT NULL) AND ((([t1].[Organization] IS NULL) AND ([t4].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t4].[Organization] IS NOT NULL) AND ([t1].[Organization] = [t4].[Organization]))))
))) AND ((([t1].[Organization] IS NULL) AND ([t3].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t3].[Organization] IS NOT NULL) AND ((([t1].[Organization] IS NULL) AND ([t3].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t3].[Organization] IS NOT NULL) AND ([t1].[Organization] = [t3].[Organization])))))
) AS [Month], (
SELECT SUM([t5].[Value])
FROM [Org] AS [t5]
WHERE ([t5].[Month] = ((
SELECT MAX([t6].[Month])
FROM [Org] AS [t6]
WHERE ([t6].[Year] = ((
SELECT MAX([t7].[Year])
FROM [Org] AS [t7]
WHERE (([t1].[Organization] IS NULL) AND ([t7].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t7].[Organization] IS NOT NULL) AND ((([t1].[Organization] IS NULL) AND ([t7].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t7].[Organization] IS NOT NULL) AND ([t1].[Organization] = [t7].[Organization]))))
))) AND ((([t1].[Organization] IS NULL) AND ([t6].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t6].[Organization] IS NOT NULL) AND ((([t1].[Organization] IS NULL) AND ([t6].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t6].[Organization] IS NOT NULL) AND ([t1].[Organization] = [t6].[Organization])))))
))) AND ((([t1].[Organization] IS NULL) AND ([t5].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t5].[Organization] IS NOT NULL) AND ((([t1].[Organization] IS NULL) AND ([t5].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t5].[Organization] IS NOT NULL) AND ([t1].[Organization] = [t5].[Organization])))))
) AS [Sum]
FROM (
SELECT [t0].[Organization]
FROM [Org] AS [t0]
GROUP BY [t0].[Organization]
) AS [t1]
Выход: