简体   繁体   中英

LINQ version of sql query with multiple joins and group by

I have a SQL Query that I'm trying to rewrite as LINQ. I've gotten through the left join aspects of it but I can't manage to combine it together with the group by stuff.

Here's the SQL Query that works and performs perfectly:

select count(c.ID) as NoteCount, count(s.StatusHistoryID) as ActionCount, p.DayGoal
from Collector_Profile p 
left join StatusHistory s on s.AppUserID = p.AppUserID 
and CONVERT(varchar(10), s.StatusDateTZ, 101) = convert(varchar(10), GETDATE(), 101)
left join Claim_Notes c on c.CollectorID = p.ID 
and CONVERT(varchar(10),c.PostDateTZ,101) = convert(varchar(10), GETDATE(), 101)
where p.ID = 1338
group by p.DayGoal

What I've got so far on the LINQ query is the following:

var query = from p in _context.Collector_Profile 
    join s in _context.StatusHistory on p.AppUserID equals s.AppUserID into gs
    join c in _context.Claim_Notes on p.ID equals c.CollectorID into gc
    from s in gs.DefaultIfEmpty()
    from c in gc.DefaultIfEmpty()
    where p.ID == CollectorID
    group p by p.DayGoal into grouped

But I don't know how to handle the date comparison truncating the date in that fashion, and I don't know how to select the results back from the query. Can someone explain how to finish this up?

You can use DbFunctions.TruncateTime method to compare date part only.

DbFunctions.TruncateTime(s.StatusDateTZ)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM