0

我有一个 Linq to Entity 查询,可以提取我的数据库的照片。我的最终目标只是获取大小并将它们总结起来,但我需要先对 ID 进行不同的调用,因为我正在对调用进行分组,显然可能有多个具有相同照片尺寸的记录,所以我不能在那个上调用 distinct . 那么我如何在 photoID 上调用不同的但拉动照片大小?谢谢你的帮助。

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)
4

1 回答 1

1

您可以使用GroupBy和 选择该组中的第一项来获得DistinctBy.

let mediaCount = g.Where(x=>x.submedia != null)
    .GroupBy(x=>x.submedia.ID)
    .Sum(mediaGroup => mediaGroup.FirstOrDefault().submedia.MediaSize)
于 2013-07-10T15:19:10.267 回答