如何将对象放入在C#中使用精致小巧的字典?(How to put objects into a di

2019-10-23 07:15发布

我有类似下面的词典:

Dictionary<string,SP> dict;

包含字符串和SP对象。

我想做到以下几点

dict = conn.Query("SELECT routine_name,created,modified FROM PROCEDURES").toDictionary (
                        row => (string)row.Routine_Name,
                        row => new SP (Name=row.Routine_Name,Created=row.created,Modified=row.modified));

上面的代码是行不通的。 如何创建上述信息的SP对象。 我有一个构造函数这三个参数。

Answer 1:

  1. 建立适当的模型

     public class SP { public string RoutineName { get; set; } public DateTime Created { get; set; } public DateTime Modified { get; set; } } 
  2. 地图查询到属性模特,选择它以字典

     Dictionary<string, SP> dictionary = connection.Query<SP>(@" SELECT routine_name AS RoutineName, created AS Created, modified AS Modified FROM PROCEDURES ").ToDictionary(m => m.RoutineName, m => m); 


文章来源: How to put objects into a dictionary using Dapper in C#?