0

Is it possible something like this? I've worked hard, but I think it's not possible, I hope someone will help me.

I have to fill the object like this,

var obj = new 
{
  parentObj = new List<object>()
  {
     outsideArray.ForEach(x=>
     {

     })
  }
}

I dont like this.

 var obj = new 
    {
        parentObj= new List<object>()
        {
            new object() { bla, bla }
        }
    }

I want to do.

var obj {
    Id =1,
    Name= "any",
    Address = new {
        userAddressList.forEach(x=> {
            Town = x.town,
            State = x.state
        }
    }
}
Caner
  • 813
  • 1
  • 12
  • 26

1 Answers1

0

The code in the second and third code snippets does not comply to C# syntax.
However, you may use LINQ to fill your array nicely similar to what shown in the third snippet:

var obj = new
{
    Id = 1,
    Name = "any",
    Address = userAddressList.Select(x => 
    {
        Town = x.town,
        State = x.state
    })
};
Mohammed Noureldin
  • 14,913
  • 17
  • 70
  • 99
  • I don't actually want it, userAddressList is Array – Caner Jan 01 '19 at 12:15
  • @caras What do you mean by that? I only converted your code to functional code according to what you showed. Even if userAddressList is an array (you probably mean it is a list or IEnumerable), it may not have the correct structure you want, therefore I used .Select() to select the desired values and assign them correctly. – Mohammed Noureldin Jan 01 '19 at 12:17
  • I mean, I already know that. I want to think of a different way. I'm sure there's a different way. – Caner Jan 01 '19 at 12:20