I have 2 models like this
public class ModelA
{
public string Name{get;set;}
public string Value{get;set;}
}
public class B
{
public List<ModelA>Items{get;set;}
}
The below input will be deserialized onto class B
{
"Items":[
{
"Name":"Food Name"
"Value":"Fries"
},
{
"Name":"Weapon Name"
"Value":"Gun"
},
{
"Name":"Vehicle Name"
"Value":"Car"
}
]
}
I have another third model like below
public class ModelValues
{
public string FoodName{get;set;}
public string WeaponName{get;set;}
public string VehicleName{get;set;}
}
I want to add the corresponding values of model A onto ModelValues class, like the value of FoodName property should get as Fries. Is there any simple way to assign the values of that class A to ModelValues. i know that we can use something like
public void AssignValues(B products)
{
ModelValues values=new ModelValues();
values.FoodName=products.Find(x=>x.Name=="Food Name").Value;
values.WeaponName=products.Find(x=>x.Name=="Weapon Name").Value;
}
In actual implementation since the items in ModelValues and the input will large i would like to avoid such line by line assigning of values. is there any ways to get this done with just few lines of code