简体   繁体   English

嵌套类C#

[英]Nested Classes C#

I have data that looks like this: 我有看起来像这样的数据:

Series1 系列1
Image1 ImageFile1 图片1图片文件1
Image2 ImageFile2 图片2图片文件2
Image3 ImageFile3 图片3图片文件3

Series2 系列2
Image1 ImageFile1 图片1图片文件1
Image2 ImageFile2 图片2图片文件2
Image3 ImageFile3 图片3图片文件3
Image4 ImageFile4 图片4

Series3 系列3
Image1 ImageFile1 图片1图片文件1

I was trying to store it using Lists and this class: 我试图使用列表和此类存储它:

  private List<SeriesClass> dcm = new List<SeriesClass>();

   public class SeriesClass
    {

      public int SeriesId { get; set; }
      public int Value1 { get; set; }
      public string Value2 { get; set; }

    }
  Loop that populates the data {
   dcm.Add(new SeriesClass { Value1 = value1, Value2 = value2, SeriesId = seriesnum});
}

Obviously not including all the code. 显然不包括所有代码。 My result is something that looks like this: 我的结果是这样的:

Series1 Image1 ImageFile1 Series1 Image1 ImageFile1
Series1 Image2 ImageFile2 Series1 Image2 ImageFile2
Series1 Image3 ImageFile3 Series1 Image3 ImageFile3

Series2 Image1 ImageFile1 Series2 Image1 ImageFile1
Series2 Image2 ImageFile2 Series2 Image2 ImageFile2
Series2 Image3 ImageFile3 Series2 Image3 ImageFile3
Series2 Image4 ImageFile4 Series2 Image4 ImageFile4

Series3 Image1 ImageFile1 Series3 Image1 ImageFile1

I was wondering if there is a good way to nest the classes so that SeriesID becomes like a keyfield containing the rest of the data. 我想知道是否有一种嵌套类的好方法,以便SeriesID变得像包含其余数据的键字段一样。 Like I can pull up a list of the Images under a particular Series. 就像我可以拉出特定系列下的图像列表。

I tried to do this: 我试图这样做:

public class SeriesClass
        {

      public int SeriesId { get; set; }

public class ImageClass{
      public int Value1 { get; set; }
      public string Value2 { get; set; }
          }

         }
  Loop that populates the data {
      dcm.Add(new SeriesClass { SeriesClass.ImageClass.Value1 = value1, SeriesClass.ImageClass. Value2 = value2, SeriesClass.SeriesId = seriesnum});
}

But the List.Add thing doesn't allow that.. Any help would be appreciated. 但是List.Add的东西不允许这样做。任何帮助将不胜感激。 I just started working with C# yesterday so this is kinda new for me. 我昨天才刚开始使用C#,所以这对我来说是个新事物。 If there is a better way to do this with jagged arrays or Dictionary or something I'd be up for that too. 如果有更好的方法可以使用锯齿状的数组或Dictionary或其他类似的方法。

Thanks :) 谢谢 :)

I believe you need to use a Dictionary. 我相信您需要使用字典。 Dictionary can use the id as a key and a list as values. 字典可以将id用作键,将列表用作值。

   private Dictionary<Integer,List<SeriesClass>> dict= new Dictionary<Integer,List<SeriesClass>>();

   public class SeriesClass
    {

      public int Value1 { get; set; }
      public string Value2 { get; set; }

    }
  Loop that populates the data {
   if ( dict.containsKey(seriesNum) ){
       dict[seriesNum].Add(new SeriesClass { Value1 = value1, Value2 = value2});
   }else{
      var list = new List<SeriesClass>();
      list.Add(new SeriesClass { Value1 = value1, Value2 = value2} );
      dcm.Add(seriesNum, list);
   }
}

I havent tested the code so there might be syntax mistakes but I hope you get the gist. 我尚未测试代码,因此可能存在语法错误,但希望您能理解。 TryGet might be better instead of using containsKey. TryGet可能比使用containsKey更好。

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

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