如何使用C#中的Dapper将对象放入字典中? [英] How to put objects into a dictionary using Dapper in C#?
本文介绍了如何使用C#中的Dapper将对象放入字典中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似以下的字典:
I have a dictionary like the following:
Dictionary<string,SP> dict;
包含一个字符串和一个SP对象。
that contains a string and a SP object.
我想执行以下操作
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对象。我有一个采用这三个参数的构造函数。
The above code is not working. How can I create a SP object with the above information. I have a constructor that takes these three parameters.
推荐答案
-
创建适当的模型
Create proper model
public class SP {
public string RoutineName { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
}
将查询映射到模型属性,将其选择为字典
Map your query to model properties, select it to dictionary
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);
这篇关于如何使用C#中的Dapper将对象放入字典中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文