I need to find the longest value from a list of objects like this...
var longestValue = list.Max(x => x.Name);
The problem is that I can not access it directly like this but this need to be made in a loop. Here is what I have so far..
public static void SetPropertyValue(this object obj, string propName, object value)
{
obj.GetType().GetProperty(propName).SetValue(obj, value, null);
}
var user = new User();
var list = new List<User>
{
new Svedea {Name = "Steve", Car = "Volkswagen"},
new Svedea {Name = "Denice Longhorn", Car = "Tesla"},
new Svedea {Name = "Rebecca", Car = "Ford"},
new Svedea {Name = "Mike O", Car = "Mercedes-Benz"}
};
var properties = user.GetType().GetProperties();
var propList = properties.Select(pi => pi.Name).ToList();
var newUser = new User();
foreach (var row in propList)
{
// Here I need to find the longest value from the list above like this...
// var longestValue = list.Max(x => x.row); // This is obviously not correct but I need to find this value needs to be found dynamically
var longestValue = list.Max(x => x.row);
newUser.SetPropertyValue(row, longestValue);
}