gpt4 book ai didi

c# - 如何在 C# 中使用 Dapper 将对象放入字典中?

转载 作者:太空宇宙 更新时间:2023-11-03 21:21:02 25 4
gpt4 key购买 nike

我有一个像下面这样的字典:

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 对象。我有一个采用这三个参数的构造函数。

最佳答案

  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);

关于c# - 如何在 C# 中使用 Dapper 将对象放入字典中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30716092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com