【问题标题】:Converting a list of dynamic obects to a Dictionary<propertyName, Values>将动态对象列表转换为 Dictionary<property Name, Values>
【发布时间】:2019-07-15 12:13:17
【问题描述】:

我正在寻找一种方法将包含动态对象的List&lt;object&gt; 转换为字典,其中键是属性名,值是该属性的不同值。

目前我使用了这段代码,但我觉得必须有更优雅/更快的方法来做到这一点?

var records = ((IEnumerable)enumerable).Cast<object>().ToList();
var result = new Dictionary<string, IList<string>>();
foreach (object record in records)
{
    foreach (var propertyName in properties)
    {
        var colValue = record.GetType().GetProperty(propertyName).GetValue(record, null);

        if (colValue != null)
        {
            if (result.ContainsKey(propertyName))
            {
                result[propertyName].Add(colValue.ToString());
            }
            else
            {
                result.Add(propertyName, new List<string>() { colValue.ToString() });
            }
        }
    }
}

【问题讨论】:

    标签: c# .net linq dictionary


    【解决方案1】:

    你在寻找这样的东西吗?

    var result = properties.ToDictionary(
        keySelector: propertyName => propertyName,
        elementSelector: propertyName => records
              .Select(record => record.GetType().GetProperty(propertyName)?.GetValue(record, null).ToString())
              .Where(colValue => colValue != null));
    

    【讨论】:

      【解决方案2】:

      您接近探针的方式很好,但您需要进行一些更改以使该方法更快。

      private static Dictionary <Type, List <PropertyInfo>> Props = new Dictionary<Type, List<PropertyInfo>> (); 
      public Dictionary <string,IList<String>> Convert(List <object> records) {                                  
          var result = new Dictionary <string,IList <string>>();                                                 
          foreach(object record in records) {                                                                    
              if (!Props.ContainsKey(record.GetType()))                                                          
                  Props.Add(record.GetType(), record.GetType().GetProperties());                                 
      
              foreach(var prop in Props[record.GetType()]) {                                                     
                  var colValue = prop.GetValue(record, null);                                                    
                  if (colValue != null) {                                                                        
                      if (result.ContainsKey(prop.Name)) {                                                       
                          result[prop.Name].Add(colValue.ToString());                                            
                      }                                                                                          
                      else {                                                                                     
                          result.Add(prop.Name, new List <string> () {colValue.ToString()});                     
                      }                                                                                          
                  }                                                                                              
              }                                                                                                  
          }                                                                                                      
          return result;                                                                                         
      }                
      

      【讨论】:

      • 更新了我的答案,看看
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-16
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      相关资源
      最近更新 更多