Home > Enterprise >  C# Linq Parent Child flatten
C# Linq Parent Child flatten

Time:09-27

I have an object like this below:

List<classA> obj = new List<classA>() {
new classA() { vCode = "100", sCode = "PB", regional = "A",  new List<classB>() {
        new classB() { amount = 100, aCode = "A100", status = 1 },
        new classB() { amount = 110, aCode = "A200", status = 1 },
        new classB() { amount = 150, aCode = "A300", status = 1 }
    }
},
new classA() { vCode = "101", sCode = "KB", regional = "A",  new List<classB>() {
        new classB() { amount = 500, aCode = "A100", status = 1 },
        new classB() { amount = 610, aCode = "A200", status = 1 },
        new classB() { amount = 750, aCode = "A300", status = 1 }
    }
},
new classA() { vCode = "102", sCode = "PB", regional = "A",  new List<classB>() {
        new classB() { amount = 100, aCode = "A100", status = 1 },
        new classB() { amount = 110, aCode = "A100", status = 1 },
        new classB() { amount = 150, aCode = "A200", status = 1 }
    }
}

}

How to flatten so the result like this below:

list of Anonymous =>  
   { vcode = "100", sCode = "PB", regional = "A", amount = 100, aCode = "A100", status = 1 },
   { vCode = "100", sCode = "PB", regional = "A", amount = 110, aCode = "A200", status = 1 },
   { vCode = "100", sCode = "PB", regional = "A", amount = 150, aCode = "A300", status = 1 },
   { vCode = "101", sCode = "KB", regional = "A", amount = 500, aCode = "A100", status = 1 },
   { vCode = "101", sCode = "KB", regional = "A", amount = 610, aCode = "A200", status = 1 },
   { vCode = "101", sCode = "KB", regional = "A", amount = 750, aCode = "A300", status = 1 },
   { vCode = "102", sCode = "PB", regional = "A", amount = 100, aCode = "A100", status = 1 },
   { vCode = "102", sCode = "PB", regional = "A", amount = 110, aCode = "A100", status = 1 },
   { vCode = "102", sCode = "PB", regional = "A", amount = 150, aCode = "A200", status = 1 }

Anyone can help me please ?

CodePudding user response:

You would use SelectMany

Projects each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one sequence.

Given

List<classA> obj = new List<classA>()
{
   new classA()
   {
      vCode = "100", sCode = "PB", regional = "A", List = new List<classB>()
      {
         new classB() {amount = 100, aCode = "A100", status = 1},
         new classB() {amount = 110, aCode = "A200", status = 1},
         new classB() {amount = 150, aCode = "A300", status = 1}
      }
   },
   new classA()
   {
      vCode = "101", sCode = "KB", regional = "A", List = new List<classB>()
      {
         new classB() {amount = 500, aCode = "A100", status = 1},
         new classB() {amount = 610, aCode = "A200", status = 1},
         new classB() {amount = 750, aCode = "A300", status = 1}
      }
   },
   new classA()
   {
      vCode = "102", sCode = "PB", regional = "A", List = new List<classB>()
      {
         new classB() {amount = 100, aCode = "A100", status = 1},
         new classB() {amount = 110, aCode = "A100", status = 1},
         new classB() {amount = 150, aCode = "A200", status = 1}
      }
   }
};

Example

var results = obj.SelectMany(x =>
   x.List.Select(y => new
   {
      x.vCode,
      x.sCode,
      x.regional,
      y.amount,
      y.aCode,
      y.status
   }));

This is example uses an anonymous type, however you can easily replace it with a concrete class

Given

internal class ClassC
{
   public string vCode { get; set; }
   public string sCode { get; set; }
   public string regional { get; set; }
   public int amount { get; set; }
   public string aCode { get; set; }
   public int status { get; set; }
}

Example

var results = obj.SelectMany(x =>
   x.List.Select(y => new ClassC
   {
      vCode =x.vCode,
      sCode = x.sCode,
      regional = x.regional,
      amount = y.amount,
      aCode = y.aCode,
      status = y.status
   }));

CodePudding user response:

You can use the .ForEach() function and the .Select() function to return a new object with the contents of both of the classes.

    List<NewModel> newList = new List<NewModel>();
    obj.ForEach(d =>
        {
            var temp = d.data.Select(s => new NewModel()
            {
                vCode = d.vCode,
                sCode = d.sCode,
                regional = d.regional,
                aCode = s.aCode,
                amount = s.amount,
                status = s.status
            });
        }
    );
  • Related