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

时间:2018-04-11 07:22:47

标签: c# dynamic-values

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

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

        }

2 个答案:

答案 0 :(得分:0)

使用LINQ OrderByDescendingName的最长值和Car的最长值对列表进行排序。然后,您可以创建一个具有最长值longestUserName的新用户Car

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

<强> longestUser:

姓名=&#34; Denice Longhorn&#34;

Car =&#34; Mercedes-Benz&#34;

修改

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

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

答案 1 :(得分:0)

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

<强>〔实施例

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