Reputation: 9378
I have a Linq to Entity query that pulls out photos of my database. My ultimate goal is just to get the size and sum them up but I need to do a distinct call on the ID first because I am grouping the call and obviously there could be multiple records with the same photo size so I cant call distinct on that. So how would I call a distinct on the photoID but pull the photo size? thanks for any help.
from x in _context.Clients
join media in _context.Mediae
on x.ID equals media.ClientID into Medias
from submedia in Medias.DefaultIfEmpty()
group new { x.ClientName, submedia } by new { x.ClientName } into g
//I would like to get the Sum of the MediaSize out of this But I call distinct on ID
let mediaCount = g.Where(x=>x.submedia != null).Select(x=>x.submedia .ID).Distinct()
//Example of what I wish would work below
//g.Where(x => x.subEventMedia != null).Select(x => x.subEventMedia.ID).Distinct().Sum(x => x.subEventMedia.MediaSize)
Upvotes: 0
Views: 1808
Reputation: 203834
You can use GroupBy
and the select the first item in that group to get the effect of a DistinctBy
.
let mediaCount = g.Where(x=>x.submedia != null)
.GroupBy(x=>x.submedia.ID)
.Sum(mediaGroup => mediaGroup.FirstOrDefault().submedia.MediaSize)
Upvotes: 1