本文介绍了一起显示具有相同GUID的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ProductDetailDTO类型的列表.

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; }
}

我已使用linq将数据绑定到列表.

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();

现在,我想一起显示具有相同GUID的所有变体(VariantName和VariantValue).我该如何实现?

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

推荐答案

您可以像这样使用GroupBy和Select:

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
                         }));

这篇关于一起显示具有相同GUID的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 13:39