如何在 C# 中使用 Dapper 将对象放入字典中?
How to put objects into a dictionary using Dapper in C#?
我有如下字典:
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 对象。我有一个采用这三个参数的构造函数。
创建合适的模型
public class SP {
public string RoutineName { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
}
将您的查询映射到模型属性,select它到字典
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);
我有如下字典:
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 对象。我有一个采用这三个参数的构造函数。
创建合适的模型
public class SP { public string RoutineName { get; set; } public DateTime Created { get; set; } public DateTime Modified { get; set; } }
将您的查询映射到模型属性,select它到字典
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);