Selecting specific columns from GroupBy list

Viewed 1486

Model:

public class Ticket {
    public Ticket();

    public int Id { get; set; }
    public virtual TicketUrgency TicketUrgency { get; set; }
    public int UrgencyId { get; set; }   
}

public class TicketUrgency {
    public TicketUrgency();
    [Key]
    public int Id { get; set; }
    [MaxLength(50)]
    [Required]
    public string Name { get; set; }
    public ICollection<Ticket> Tickets { get; set; }
}

I have the following linq statement:

 var model = Entities
                .Include(x => x.TicketUrgency)
                .GroupBy(x => x.UrgencyId)
                .Select(g => new {
                    id = g.Key,
                    count = g.Count(),
                    name = g.FirstOrDefault(u => u.UrgencyId == g.Key).TicketUrgency.Name
                });

I want to Group Entities by UrgencyId and then return the Key (UrgencyId), and also count of the items in a single group and show the name of the Urgency.

When I run it, the query just gets hung up without any exceptions.

4 Answers
Related