在EF Core中对linq进行分组

我有以下数据:

图片表:

Id   Name    CategoryId
1    image1  1 
2    image2  1

类别表:

Id  CategoryName   Description
1   Team Building  TB 2020

我在linq中编写查询,以获取图像的所有类别以及每个类别中的关联图像。

var query = from p in _context.ImagesCategory
                            join s in _context.ImagesGallery on p.Id equals s.CategoryId into groupcat
                            from s in groupcat
                            select new CategoryGalleryDto {  Id = p.Id, CategoryName = p.ImageCategory, CategoryDescription = p.ImageDescription, ImagesList = new List<ImageGalleryDto> { new ImageGalleryDto { ImageName = s.ImageName } } };

                var grouping = query.ToLookup(e => e.Id).ToList();

尽管我只有一个类别,但查询仍返回三个类别。请帮忙

评论
  • 刺眼旳情侣
    刺眼旳情侣 回复

    将p.Id更改为p.CategoryId

    var query = from p in _context.ImagesCategory
                            join s in _context.ImagesGallery on p.CategoryId equals s.CategoryId into groupcat
                            ...