Linq: Group by one column but must distinct other column

In my linq query I want to query the service table which has a visitId and a deviceId. I want to be able to group by the visit but have the deviceId distinct. Currently with my setup I can group by visitId but I'm having trouble showing disitnct devices in the result when the visit is the same.

from s in db.Services.Where(condition)
                 where s.Active && (hasSupportGeneralAccess || s.Visit.Site.Branch.Users.Any(u => u.Id == userId)) && s.Device != null
                 orderby s.Visit.Start descending
                 group s by s.Visit into newgroup
                 select newgroup.FirstOrDefault()

Any Help is greatly appreciated!



Read more here: https://stackoverflow.com/questions/64591699/linq-group-by-one-column-but-must-distinct-other-column

Content Attribution

This content was originally published by Harjot Singh at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: