我正在尝试在下面的linq中转换http://sqlfiddle.com/#!6/a1c8d/2中的SQL。预期的结果是sqlfiddle,但我的LINQ返回更多的行。
PS:在sqlfiddle中,字段减少,不会增加污染,并专注于我的问题。resultado.Dados =
(
from a in db.AgendaHorario
join b in db.Agenda on a.AgendaID equals b.AgendaID
select new
{
a.AgendaID,
Horario = a.Horario,
Controle = a.Controle,
Cor = b.Cor,
Agenda = b.Sigla
}).AsEnumerable()
.GroupBy(g => new
{
g.AgendaID,
Horario = g.Horario.ToString("dd/MM/yyyy"),
Data = g.Horario.ToString("yyyy-MM-dd"),
g.Controle,
g.Agenda,
g.Cor
})
.Select(s => new
{
id = s.Key.AgendaID,
title = s.Key.Agenda,
start = s.Key.Data,
color = String.IsNullOrEmpty(s.Key.Cor) ? "3a87ad" : s.Key.Cor,
className = "",
someKey = 1,
allDay = false,
Resultado0 = s.Sum(m => m.Controle == "L" ? 1 : 0).ToString(),
Resultado1 = s.Sum(m => m.Controle == "B" ? 1 : 0).ToString()
});
答案 0 :(得分:1)
根据评论,这解决了如何在Linq中重复您的SqlFiddle的问题。请注意,对String Date的投影无法直接转换为Sql,因此我必须尽早使用AsEnumerable()
实现(显然,在您的实际查询中,在实现之前应用任何过滤器!)。您可以使用SqlFunctions对日期部分进行分组,例如3个SqlFunctions.DatePart
的应用程序将允许您按dd,MM和YYYY分组
var dados = db.AgendaHorarios1
.AsEnumerable()
.GroupBy(ah => ah.Horario.ToString("dd/MM/yyyy"))
.Select(g => new {Horario = g.Key,
Livre = g.Count(x => x.Controle == "L"),
Bloq = g.Count(x => x.Controle == "B"),
Aged = g.Count(x => x.Controle == "A")});