简体   繁体   English

按 Linq EF Core 中的项目类别分组

[英]Grouping by category of items in Linq EF Core

How to fetch records and group under each respective category in linq-to-sql or linq extension in Entity framework core 3.0 and project them into their respective DTOs?如何在实体框架核心 3.0 中的 linq-to-sql 或 linq 扩展中获取每个类别下的记录和组,并将它们投影到各自的 DTO 中?

I have the following DTOs我有以下 DTO

public class CategoryGalleryDto
{
    public int Id { get; set; }
    public string CategoryName { get; set; }

    public List<ImageGalleryDto> ImagesList { get; set; }
}

public class ImageGalleryDto
{
    public int Id { get; set; }

    public string ImageName { get; set; }

    public int CategoryId { get; set; }
}

I have the following records:我有以下记录:

ImageGallery Table图片库表

Id  ImageName   CategoryId
1   image1  1
2   image2  1
4   image3  1
5   image1  2
6   image2  2

I want to have a list of categories and a list of its corresponding images with each category.我想要一个类别列表以及每个类别对应的图像列表。 How can I achieve that in linq?如何在 linq 中实现这一点? Please help, I'm really stuck with this.请帮忙,我真的坚持这个。

ImageCategory Table图像类别表

Id  ImageCategory   ImageDescription    UploadedDate
1   Team Building   Team Building Event     2020-05-11 00:00:00.000
2   Christmas   Christmas Lunch 2020    2020-05-11 00:00:00.000

My code:我的代码:

public List<CategoryGalleryDto> GetAllImages()
{
    var imageList = new List<CategoryGalleryDto>();

    try
    {
        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 { Categories = p.Id, Images = s };

        var grouping = query.ToLookup(e => e.Categories);

    }
    catch (Exception ex)
    {
        throw ex;
    }
    return imageList;
}

You can use linq like this:您可以像这样使用 linq :

var query = _context.ImagesCategory.Join(_context.ImagesGallery,
                                 imagesCategory => imagesCategory.Id,
                                 imagesGallery => imagesGallery.CategoryId,
                                 (imagesCategory, imagesGallery) => new { ImagesCategory = imagesCategory, ImagesList = imagesGallery })
                                .GroupBy(
                                            p => p.ImagesCategory,  
                                            p => p.ImagesList,
                                           (imagesCategory, imagesList) => new { ImagesCategory = imagesCategory, ImageList = imagesList.ToList() })
                                .Select(x => new { Id = x.ImagesCategory.Id, CategoryName = x.ImagesCategory.ImageCategory, ImageList = x.ImageList })
                                .ToList();

You can convert entity to dto like this:您可以像这样将实体转换为 dto:

foreach (var item in query)
{
   CategoryGalleryDto categoryGalleryDto = new CategoryGalleryDto();

   categoryGalleryDto.Id           = item.Id;
   categoryGalleryDto.CategoryName = item.CategoryName;
   categoryGalleryDto.ImagesList   = new List<ImageGalleryDto>();

   foreach (var image in item.ImageList)
   {
       categoryGalleryDto.ImagesList.Add(new ImageGalleryDto()
       {
          Id         = image.Id,
          ImageName  = image.ImageName,
          CategoryId = image.CategoryId
       });
   }

   imageList.Add(categoryGalleryDto);
}

Or you can use AutoMapper library.或者您可以使用 AutoMapper 库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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