c# - 如何将数组转换为字典

标签 c# linq dictionary c#-6.0

如何将以下fieldList数组转换为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。

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


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

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

关于c# - 如何将数组转换为字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42904329/

相关文章:

python - 子类化 python 的字典,重写 __setitem__ 不保留新值

C#正则表达式麻烦

c# - CaSTLe Windsor ASP.NET MVC 4 和 Web API 相同的应用程序。仅当 MVC Controller 没有依赖项时?

c# - UWP:替代 Grid.IsSharedSizeScope 和 SharedSizeGroup

c# - 动态创建匿名类型的属性

python - 在 Python 中从空格分隔的 key=value 字符串创建字典

c# - 在c#中读取cookie

c# - 从 IQueryable<T> 中删除 OrderBy

c# - Linq 比较 XML 值

java - 如何检查 HashMap 中的值