我想一些SQL代碼轉換爲C#中的LINQ:轉換SQL的LINQ C#
SELECT Username, Count(Ticket.TicketId) as 'Tickets Completed'
FROM Ticket
INNER JOIN TicketStatus ON Ticket.TicketStatusID = TicketStatus.TicketStatusID
INNER JOIN Membership ON Ticket.CompletedBy = Membership.UserId
WHERE Ticket.ClosedDate >= @StartDate
and Ticket.ClosedDate <= @EndDate
GROUP BY Username
ORDER BY 'Tickets Completed' DESC
,顯示
Paul 6
Mike 4
Donna 3
Elliot 2
我試圖用Linqer這讓這更復雜,並沒有回報任何結果:
var query = from Ticket in data.Tickets
join Membership in data.Memberships on new { CompletedBy = Guid.Parse(Ticket.CompletedBy.ToString()) } equals new { CompletedBy = Membership.UserId }
where
Ticket.ClosedDate >= StartDate &&
Ticket.ClosedDate <= EndDate
group new { Membership, Ticket } by new
{
Membership.Username
} into g
orderby
"Tickets Completed" descending
select new
{
Username = g.Key.Username,
Completed = g.Count(p => p.Ticket.TicketID > 0)
};
您的幫助,將不勝感激。
感謝
你確定數據不是空的嗎?你在使用ORM嗎? –