I have three class :
public class MyClass
{
public Head Head { get; set; }
public List<Car> Cars { get; set; }
}
public class Head
{
public int Id { get; set; }
}
public class Car
{
public string Color { get; set; }
public bool Active { get; set; }
}
And one instance List<object>
:
List<object> o = new List<object>()
{
new Head()
{
Id = 1
},
new Car()
{
Color = "Yellow",
Active = false
},
new Car()
{
Color = "Black",
Active = true
},
new Car()
{
Color = "White",
Active = false
}
};
I want to convert my instance to a instance of my class MyClass
(first index = property Head of my class MyClass
and all Car = property List)
What is the best way to do this ?
Linq will help you here. Use OfType extension method to get instances of certain type.
new MyClass{
Head = o.OfType<Head>().First(),
Cars = o.OfType<Car>().ToList()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.