Борьба за преобразование SQL-запроса в его эквивалент LINQ - множественные объединения, группировки и агрегатные функции - PullRequest
0 голосов
/ 02 мая 2019

У меня есть следующий запрос, который дает ожидаемые результаты в SQL Server Management Studio:

SELECT 
    u.DisplayName,
    up.ColorPreferences,
    SUM(rt.Score) AS Points,
    COUNT(*) AS Plans,
    MAX(pl.Created) AS MaxDate
FROM 
    [dbo].[Users] u
INNER JOIN 
    [dbo].[PlanLogs] pl ON u.Id = pl.UserId
INNER JOIN 
    [dbo].[ResourceTypes] rt ON pl.ResourceTypeId = rt.Id
INNER JOIN 
    [dbo].[UserProfile] up ON pl.UserId = up.UserId
GROUP BY 
    u.DisplayName, up.ColorPreferences;

a У меня есть следующий рабочий запрос linq:

from u in _context.Users
join pl in _context.PlanLogs on u.Id equals pl.UserId
join rt in _context.ResourceTypes on pl.ResourceTypeId equals rt.ID
join up in _context.UserProfile on pl.UserId equals up.UserId
group rt by new { u.DisplayName, up.ColorPreferences} into g
select new
{
DisplayName = g.Key.DisplayName,
ColorPrefs = g.Key.ColorPreferences,
Points = g.Sum(x => x.Score),
Plans = g.Count()
};

Как видите, отсутствует MaxDate. Я не могу получить доступ к MaxDate, потому что g содержит свойства из RT. Я пробовал следующее, и я получаю «Значение не попадает в ожидаемый диапазон»

from u in _context.Users
join pl in _context.PlanLogs on u.Id equals pl.UserId
join rt in _context.ResourceTypes on pl.ResourceTypeId equals rt.ID
join up in _context.UserProfile on pl.UserId equals up.UserId
group new { rt, pl } by new { u.DisplayName, up.ColorPreferences} into g
select new
{
DisplayName = g.Key.DisplayName,
ColorPrefs = g.Key.ColorPreferences,
Points = g.Sum(x => x.rt.Score),
Plans = g.Count()
MaxDate = g.Max(m => m.pl.Created)
};

Как добавить MaxDate к результатам?

Спасибо

Ответы [ 2 ]

0 голосов
/ 03 мая 2019

Я довел это до конца. Мне нужно было передать конкретные столбцы в группу, а не всю таблицу:

group new { rt.Score, pl.Created } by..

вместо

group new { rt, pl } by...

Рабочий запрос:

from u in _context.Users
join pl in _context.PlanLogs on u.Id equals pl.UserId
join rt in _context.ResourceTypes on pl.ResourceTypeId equals rt.ID
join up in _context.UserProfile on pl.UserId equals up.UserId
group new { rt.Score, pl.Created } by new { u.DisplayName, up.ColorPreferences } into g
select new
{
DisplayName = g.Key.DisplayName,
ColorPrefs = g.Key.ColorPreferences,
Points = g.Sum(i => i.Score),
Plans = g.Count(),
MaxCreated = g.Max(i => i.Created).ToString("dd/MM/yyyy HH:mm")
}
0 голосов
/ 02 мая 2019

Вы пытались получить доступ к максимальному значению из pl.created при первом запросе linq?почему вы группируете по RT, а не весь результат?попробуйте это вместо:

from u in _context.Users
join pl in _context.PlanLogs on u.Id equals pl.UserId
join rt in _context.ResourceTypes on pl.ResourceTypeId equals rt.ID
join up in _context.UserProfile on pl.UserId equals up.UserId
group u by new { u.DisplayName, up.ColorPreferences} into g
select new
{
  DisplayName = g.Key.DisplayName,
  ColorPrefs = g.Key.ColorPreferences,
  Points = g.Sum(x => x.Score),
  Plans = g.Count(),
  MaxDate = g.Max(m => m.pl.Created)
};
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...