【问题标题】:Check if some items are same in a c# list检查 c# 列表中的某些项目是否相同
【发布时间】:2023-03-03 03:10:02
【问题描述】:

我想根据列表中存在的项目检查列表中的某些项目是否相同。

List<ProductDetailDTO> productDTOs;

ProductDetailDTO 是 -

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

现在,我想同时显示所有具有相同 GUID 的 VariantName 和 VariantValue。

我怎样才能做到这一点?

【问题讨论】:

标签: c# list entity-framework linq


【解决方案1】:

试试这个

productDTOs.GroupBy(x => x.GUID,(key,item) => new
            {
                VariantName= item.Select(y=>y.VariantName),
                VariantValue = item.Select(y => y.VariantValue),

            }).ToList()

【讨论】:

  • 对 guid 解析的次要建议。请注意,它们现在作为字符串存储在 DTO 中。可能是未被抓住的骗子。
  • @clarkitect 已编辑!!
  • 如何将所有具有相同 GUID 的变体显示在一起?
  • @mukeshkudi 不,它没有。我真正想要的是一起显示具有相同 GUID 的变体。
  • @SuyashGupta 已编辑!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多