Get specific part of dictionary instance

I need help to convert specific part of the dictionary instance, let's say for example I have this model instance

public class MyModel
{
  public int id {get;set;}
  public List<ClassC> classc {get; set;}
  public Dictionary<ClassA,ClassB> {get; set;}
}

on this MyModel instance, I need to remove ClassA and convert ClassB in dictionary instance to list and I will have this result. Is there a direct way to do this or I really need to extract each records and transfer each to new model class?

public class newMyModel
{
  public int id {get;set;}
  public List<ClassC> classc {get;set;}
  public List<ClassB> {get; set;}
}

2 answers

  • answered 2019-04-15 06:06 Michał Turczyn

    You can define constructor of newMyModel that will accept as its parameter object of type MyModel:

    public newMyModel(MyModel oldModel)
    {
      this.id = oldModel.id;
      this.classc = oldModel.classc;
      this.classb = oldModel.dict.Values.ToList();
    }
    

    I assumed that Dictionary field in MyModel class is named dict and List<ClassB> in newMyModel is named classb.

  • answered 2019-04-15 06:14 Just Shadow

    All you need is to use Values.ToList() which comes with linq.
    Here is a complete sample for your case:

    using System.Collections.Generic;
    using System.Linq; // <- Don't forget this
    
    public class Program
    {
        public class ClassA {
            public int A {get; set;}
        }
        public class ClassB {
            public int B {get; set;}
        }
        public class ClassC {
            public int C {get; set;}
        }
    
        public class MyModel
        {
            public int id {get;set;}
            public List<ClassC> classc {get; set;}
            public Dictionary<ClassA,ClassB> dic {get; set;}
        }
    
        public class NewMyModel
        {
            public int id {get;set;}
            public List<ClassC> classc {get;set;}
            public List<ClassB> listb {get; set;}
        }
    
        public static void Main()
        {
            // Initializing MyModel
            MyModel a = new MyModel(){
                id = 1,
                classc = new List<ClassC>(){
                    new ClassC(){
                        C = 1
                    }
                },
                dic = new Dictionary<ClassA,ClassB>(){
                    {
                        new ClassA(){
                            A = 1
                        },
                        new ClassB() {
                            B = 1
                        }
                    }
                }
            };
    
            NewMyModel b = new NewMyModel() {
                id = a.id,
                classc = a.classc,
                // Converting values of the dictionary to list
                // Note: Values contains ClassB type of objects
                listb = a.dic.Values.ToList()
            };
        }
    }
    

    Note:
    Don't forget to import System.Linq to make ToList() function available.