簡體   English   中英

如何在C#中按記錄分組

[英]How to group by records in c#

我正在嘗試使用LINQ支持的Group By方法。

我有這個課

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

  public string Name {get;set;}

  public string Value {get;set;} 

}

我有一個將檢索IList的服務方法

var attributes = _service.GetAll();
Id  Name    Value
7   Color   Black
7   Color   White
220 Size    16

現在我還有另一個拖車課

一個是

public class AttributeResourceModelSubItem
{
   public string Name {get;set;}

   public List<AttributeValueResourceModelSubItem> values { get; set; }
}

public class AttributeValueResourceModelSubItem
{
   public int Id;
   public string Name {get;set;}
}

我試圖遍歷屬性列表。 如果屬性id是相同的,我想將id =到ID的記錄插入AttributeValueResourceModelSubItem中,其中id = 1並且Name將等於屬性值。

這是我到目前為止所獲得的。

private IList<AttributeResourceModelSubItem> FormatAttributes(IList<Attribute> attributes)
{
    Dictionary<int, Attribute> baseTypes = new Dictionary<int, Attribute>();
    AttributeResourceModelSubItem attributeResourceModelSubItem = null;
    var list = new IList<AttributeResourceModelSubItem>();
    foreach (var item in attributes)
    {
        if (!baseTypes.ContainsKey(item.Id))
        {
            attributeResourceModelSubItem = new AttributeResourceModelSubItem()
            attributeResourceModelSubItem.key = item.Name;
            attributeResourceModelSubItem.values.Add(new AttributeValueResourceModelSubItem()
            {
                id = 1,
                name = item.Value
            });
            list.Add(attributeResourceModelSubItem);
        }
        baseTypes.Add(item.Id, item);
    }
    return list;
}

任何幫助表示贊賞。

從您的示例中還不清楚您實際上要做什么,但這就是我的要旨。

private IEnumerable<AttributeResourceModelSubItem> FormatAttributes(IEnumerable<Attribute> attributes)
{
    return attributes.GroupBy(c => c.Id)
                     .Select(c => new AttributeResourceModelSubItem()
                                  {
                                      key = c.First().Name,
                                      values = c.Select(x => new AttributeValueResourceModelSubItem()
                                                             {
                                                                 id = 1,
                                                                 name = x.value
                                                             }).ToList();
                                  });
}

您也絕對不應使用“ Attribute ”一詞作為類名。 那已經是一個.NET類。

我承認我不太了解id = 1部分,但是我從您的代碼中了解了這一點。 id分組然后嘗試取name也似乎很奇怪,但這又是您的name

實際上,如果您想按名稱分組並采用id ,這更有意義,那么您將需要交換一些信息。 誠然,這種結構在我看來仍然有些奇怪,但是希望這會使您更接近目標。

private IEnumerable<AttributeResourceModelSubItem> FormatAttributes(IEnumerable<Attribute> attributes)
{
    return attributes.GroupBy(c => c.name)
                     .Select(c => new AttributeResourceModelSubItem()
                                  {
                                      key = c.Key,
                                      values = c.Select((item, index) => new AttributeValueResourceModelSubItem()
                                                             {
                                                                 id = index + 1,
                                                                 name = item.value
                                                             }).ToList();
                                  });
}

我還為每個values列表中的每個元素從id = 1開始使id = 1遞增。 您可能希望將其作為item.Id ,甚至只是您原來的1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM