I am trying to recreate this sql into linq.
select a.Name Agency,
COUNT(CASE when p.AssignedAgencyId = a.Id then 1 end) Submissions,
COUNT(CASE when p.AssignedAgencyId = a.Id AND p.SubmissionStatusId= 2 then 1 end) Rejected,
COUNT(CASE when p.AssignedAgencyId = a.Id AND p.SubmissionStatusId= 3 then 1 end) Denied
FROM Agencies a
join projects p on p.AssignedAgencyId = a.Id
Group By a.Name
this is what I've come up with but I don't understand how to get a value from a subquery in this way
var agencyResults = (
from a in _context.Agencies
join p in _context.Projects on a.Id equals p.AssignedAgencyId
where (data.AgencyId == null || a.Id == data.AgencyId)
group p by p.AssignedAgencyId into g
select new
{
AgencyName = (from aa in _context.Agencies
where (aa.Id == data.AgencyId)
select aa),
TotalCount = g.Count(),
RejectedCount = g.Count(e => e.SubmissionStatusId == 2),
DeniedCount = g.Count(e => e.SubmissionStatusId == 3)
});
this is the result set I'm looking for.