【问题标题】:c# add object array to ICollection - convert dto -> modelc# 将对象数组添加到 ICollection - 转换 dto -> 模型
【发布时间】:2015-10-14 03:43:03
【问题描述】:

我需要转换这个 DTO

public class MyDTO
{
   [JsonProperty("studentInfo")]
   public StudentInfo studentInfo {get; set; }

   public class StudentInfo
   { 
      [JsonProperty("others")]
      public ICollection<AnotherDTO[]> Others { get; set; }
   }
   public class AnotherDTO
   {
       [JsonProperty("name")]
       public string name { get; set; }
   }

}

到这个模型

public class MyModel
{
   public StudentInfo studentInfo {get; set; }

   public class StudentInfo
   { 
      public ICollection<Another[]> Others { get; set; }
   }
   public class Another
   {
       public string name { get; set; }
   }

}

我正在挂断 ICollection。这是我试图填充 ICollection Others 的地方。

 private static ICollection<MyModel.Another[]> getAnothers(MyDTO myDTO, MyModel myModel)
 {  
    List<MyModel.Another> myList = new List<MyModel.Another>();
    foreach(var x in myDTO.studentInfo.Others)
    {
      foreach(var y in x)
      {
         myList.Add(new MyModel.Another
         {
             name = y.name
         });
      }

    }
 }
 MyModel.Another[] newList;
 newList = myList.ToArray();
 ICollection<MyDTO.AnotherDTO[]> ic = (ICollection<MyModel.Another[]>newList.Cast<MyModel.Another[]>().ToList();

最后一行给了我以下错误:

无法将 MyModel 类型的对象转换为 MyModel[] 类型。

我将不胜感激。

【问题讨论】:

    标签: c# dto icollection


    【解决方案1】:

    我建议使用AutoMapper,而不是手动映射对象。 AutoMapper 是一个简单的小库,旨在解决一个看似复杂的问题——摆脱将一个对象映射到另一个对象的代码。您还可以获取 AutoMapper 的 Nuget 包

    只需定义对象的映射 -

    Mapper.CreateMap<MyDTO, MyModel>();
    

    ..然后像这样简单地映射对象 -

    var myModelObject = Mapper.Map<MyModel>(myDtoObject);
    

    请参考this入门链接。

    【讨论】:

      【解决方案2】:

      您可以使用 select 语句来转换集合。

      var myDto = new MyDto
      {
          studentInfo = new MyModel.StudentInfo
          {
              Others = new List<AnotherDTO[]>
              {
                  new[] 
                  {
                     new AnotherDTO { Name = "a" },
                     new AnotherDTO { Name = "b" }
                  },
                  new[] 
                  {
                     new AnotherDTO { Name = "x" },
                     new AnotherDTO { Name = "y" }
                  }
              }
          }
      
          var model = new MyModel
          {
              studentInfo = new MyModel.StudentInfo
              {
                  Others = dto.StudentInfo.Others
                     .Select(otherDtoArray =>
                          otherDtoArray
                            .Select(otherDto => new MyModel.Other {Name = otherDto.Name})).ToList()
              }
      

      【讨论】:

        【解决方案3】:

        感谢大家的 cmets。我的主要问题是尝试转换 ICollection。

        我最终将模型中的 ICollection 更改为简单的 List。而且效果很好。我无法按照建议使用 AutoMapper,但我确实看到了它对于像我这样的情况的价值。

        【讨论】:

          【解决方案4】:

          你的代码有各种各样的错误。

          foreach(var x in myDTO.studentInfo.Others)
          {
            foreach(var y in x)
            {
               myList.Add(new MyModel.Another
               {
                   name = y.name
               });
            }
          

          为什么需要第二个 foreach 循环?您的第一个 foreach 循环已经在您的 Others 中循环(类型:ICollection)。每个“x”都是 AnotherDTO 的一个实例。

          其次,以下是完全错误的:

           MyModel.Another[] newList;
           newList = myList.ToArray();
           ICollection<MyDTO.AnotherDTO[]> ic = (ICollection<MyModel.Another[]>newList.Cast<MyModel.Another[]>().ToList();
          

          MyList 已经是一个列表。然后,您尝试将其转换为数组,然后再转换回列表。这样做没有意义。

          另外,您也可以根本不使用ICollection。它很重。只需使用List

          另外,不要用简单的字母“a”、“b”、“x”、“y”等来命名变量……一旦你进入就业市场,你会被同事骂的.

          这是您的代码的更正版本:

          public class SomeClass
          {
           public static List<Another> MapAnotherDtoToAnother(List<AnotherDTO> others)
           {  
              List<Another> anotherList = new List<Another>();
              foreach(var anotherDto in others)
              {
                  Another another = new Another();
          
                  /* Populate fields of `another` whatever they are from `anotherDto` */
                  another.FirstName = anotherDto.FirstName;
          
                  anotherList.Add(another);
              }
          
              return anotherList;
          
           }
          }
          

          要将 ICollection 转换为 List,只需执行以下操作:

          /* Convert your ICollection to a List */
          List<AnotherDTO> AnotherDTOList = myDto.StudentInfo.Others.Cast<AnotherDTO>().ToList()
          
          /* Use your static function get your non-dto counterpart */
          List<Another> anothers = SomeClass.MapAnotherDtoToAnother(AnotherDTOList);
          

          希望对您有所帮助。 祝你好运!

          【讨论】:

          • Others 是ICollection&lt;AnotherDTO[]&gt;(即数组列表)这是否是一个好的设计决策是另一个问题,但就目前的代码而言,他绝对需要第二个 foreach。
          猜你喜欢
          • 2017-02-22
          • 1970-01-01
          • 2020-11-04
          • 1970-01-01
          • 1970-01-01
          • 2011-04-12
          • 2023-01-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多