按字符串在对象列表中查找值

find value in List of object by string

我需要从这样的对象列表中找到最长的值...

var longestValue = list.Max(x => x.Name);

问题是我不能像这样直接访问它,但这需要在循环中进行。这是我目前所拥有的..

        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);

        }

使用 LINQ OrderByDescendingName 的最长值和 Car 的最长值对列表进行排序。然后您可以创建一个新用户 longestUser,其最长值为 NameCar:

var longestName = list.OrderByDescending(x => x.Name.Length).First().Name;
var longestCar = list.OrderByDescending(x => x.Car.Length).First().Car;
var longestUser = new User() { Name = longestName, Car = longestCar };

最长用户:

姓名="Denice Longhorn"

汽车="Mercedes-Benz"

编辑:

我已经修改了上面的解决方案,以便您可以使用字符串值访问 属性 名称:

var longestNameUser = list.OrderByDescending(x => x.GetType().GetProperty("Name").GetValue(x, null).ToString().Length).First();
var longestName = longestNameUser.GetType().GetProperty("Name").GetValue(longestNameUser, null);

var longestCarUser = list.OrderByDescending(x => x.GetType().GetProperty("Car").GetValue(x, null).ToString().Length).First();
var longestCar = longestCarUser.GetType().GetProperty("Car").GetValue(longestCarUser, null);

var newUser = new User();
PropertyInfo propertyInfo;

propertyInfo = newUser.GetType().GetProperty("Name");
propertyInfo.SetValue(newUser, Convert.ChangeType(longestName, propertyInfo.PropertyType), null);

propertyInfo = newUser.GetType().GetProperty("Car");
propertyInfo.SetValue(newUser, Convert.ChangeType(longestCar, propertyInfo.PropertyType), null);

你修改的for循环:

var newUser = new User();
PropertyInfo propertyInfo;

foreach (var row in propList)
{
    var longestValueUser = list.OrderByDescending(x => x.GetType().GetProperty(row).GetValue(x, null).ToString().Length).First();
    var longestValue = longestValueUser.GetType().GetProperty(row).GetValue(longestValueUser, null);
    propertyInfo = newUser.GetType().GetProperty(row);
    propertyInfo.SetValue(newUser, Convert.ChangeType(longestValue, propertyInfo.PropertyType), null);
}

你可以通过混合泛型和反射来做这样的事情

例子

public static T CreateType<T>(List<T> list) where T : new ()
{
   var obj = new T();

   var type = typeof(T);
   var properties = type.GetProperties();

   foreach (var prop in properties)
   {
      var value = list.Select(x => x.GetType()
                                    .GetProperties()
                                    .First(y => y == prop)
                                    .GetValue(x) as string)
                      .OrderByDescending(x => x.Length)
                      .First();

      var propInstance = obj.GetType()
                            .GetProperties()
                            .First(x => x == prop);

      propInstance.SetValue(obj, value);
   }

   return obj;
}

用法

var someList = new List<SomeClass>()
                  {
                     new SomeClass()
                        {
                           Name = "asd",
                           Car = "sdfsdf"
                        },
                     new SomeClass()
                        {
                           Name = "dfgfdg",
                           Car = "dfgdf"
                        }
                  };


var SomeClass = CreateType(someList);

输出

some value 1 longer, Some value 2 longer

Full demo here