如何将数组转换为字典

如何将数组转换为字典

本文介绍了如何将数组转换为字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将下面的fieldList数组转换为Dictionary<string,column> (i.e. Dictionary of name property of column class as key and value property of dictionary as column object)的字典.

How to convert below fieldList array to dictionary as Dictionary<string,column> (i.e. Dictionary of name property of column class as key and value property of dictionary as column object).

public class columninfo
{
 public string name {get;set;}
 public column[] fieldList {get;set;}
}

public class column
{
  public string name {get;set;}
  public string fieldname {get;set}
  public string format {get;set;}
}

推荐答案

您可以为此使用linq lambda.

You can use a linq lambda for this.

column[] columns = getColumninfo();
columns.ToDictionary(x => x.name, y => y);

通过添加StringComparer.OrdinalIgnoreCase,您可以确保列名查找不区分大小写.

By adding StringComparer.OrdinalIgnoreCase you can ensure that the column name lookup is case insensitive.

columns.ToDictionary(x => x.name, y => y, StringComparer.OrdinalIgnoreCase);

这篇关于如何将数组转换为字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:34