[英]LINQ, double join with count and orderby
LINQ newbie struggling with a query which would be very easy in regular SQL, but for some reason is causing me serious ache when trying to express in lambda/query. LINQ新手在查询中挣扎,这在常规SQL中非常容易,但是由于某种原因,在尝试使用lambda / query进行表达时,我感到非常痛苦。
I have three objects: Organization 我有三个对象:组织
public class Organization
{
public int OrganizationId { get; set; }
public ICollection<Employee> Employees { get; set; }
}
Employee 雇员
public class Employee
{
public int EmployeeId { get; set; }
public Organization Organization { get; set; }
public ICollection<Ticket> Tickets { get; set; }
}
Ticket 票
public class Ticket
{
public int TicketId { get; set; }
public Employee Employee { get; set; }
}
Using LINQ, I need to retrieve an ordered list of Organizations by the number of Tickets they have, as an IQueryable, preferable. 使用LINQ,我需要按组织的票证数量来检索组织的有序列表,作为IQueryable,它是可取的。
This is what I have come up with so far: 到目前为止,这是我想出的:
var q = Organizations.Join
(
Employees,
org => org.OrganizationId,
emp => emp.Organization.OrganizationId,
(org, emp) => new {org, emp}
)
.Join
(
Tickets,
t => t.emp.EmployeeId,
ticket => ticket.Employee.EmployeeId,
(t, ticket) => new {t, ticket}
)
.GroupBy(t => t.t.org.OrganizationId)
.Select
(
t => new { OrganizationId = t.Key, TCount = t.Count() }
)
.OrderByDescending(t => t.TCount).Dump()
So I have the ID's of the Organizations and the count. 所以我有组织的ID和数量。 How would I go about retrieving the full Organization objects?
我将如何检索整个组织对象?
您不需要使用join,因为每个类都有集合。
Organizations.OrderByDescending(o => o.Employees.Sum(e => e.Tickets.Count()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.