Display fields with the same GUIDs together [duplicate]

你说的曾经没有我的故事 提交于 2019-12-25 01:35:23

问题


I have a list of type ProductDetailDTO.

List<ProductDetailDTO> productDTOs;

public class ProductDetailDTO
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
    public byte[] Image { get; set; }
    public string Description { get; set; }
    public string Brand { get; set; }
    public string GUID { get; set; }
    public string VariantName { get; set; }
    public string VariantValue { get; set; }
    public decimal Price { get; set; }
}

I have used linq to bind the data to the list.

var productDetails = (from product in ekartEntities.Products
                                  join productImage in ekartEntities.ProductImages on product.ProductId equals productImage.ProductId
                                  join category in ekartEntities.ProductCategories on product.Category equals category.CategoryId
                                  join mapping in ekartEntities.ProductVariantMappings on product.ProductId equals mapping.ProductId
                                  join variant in ekartEntities.ProductVariants on mapping.ProductVariantId equals variant.ProductVariantId
                                  join inventory in ekartEntities.Inventories on mapping.GUID equals inventory.Guid
                                  where product.ProductId == productDetailDTO.ProductId
                                  select new ProductDetailDTO()
                                  {
                                      ProductId = product.ProductId,
                                      Name = product.Name,
                                      Category = category.Name,
                                      Description = product.Description,
                                      Brand = product.Brand,
                                      Image = productImage.Image,
                                      GUID = mapping.GUID.ToString(),
                                      VariantName = variant.Name,
                                      VariantValue = mapping.Value,
                                      Price = inventory.Price
                                  }).ToList();

Now, I want to display all the variants (VariantName and VariantValue) with the same GUIDs together. How can I achieve that?


回答1:


You can use GroupBy and Select like this:

 var variants = productDTOs
                   .GroupBy(k => k.GUID)
                   .Select(v => v
                         .Select(variant => new 
                         {
                             variant.VariantName, 
                             variant.VariantValue
                         }));



回答2:


You can use Group By

group p by p.GUID into g
select new { Id = g.Key, ProductDetail = g.ToList()).ToList();

If you have tables before group by, then you can add new object in the group itself

group new { p.xyz, n.xyz }
by new { p.GUID } into g

otherwise use let to save the intermediate object in an object and do grouping on it



来源:https://stackoverflow.com/questions/52252391/display-fields-with-the-same-guids-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!