gpt4 book ai didi

c# - 转换动态 Dapper 查询

转载 作者:太空狗 更新时间:2023-10-30 00:31:14 25 4
gpt4 key购买 nike

我正在尝试将动态查询从 dapper 转换为 IDictionary

        const string sql = @"SELECT Name, Street FROM Contact";
dynamic query = null;

using (var cn = Connection)
{
query = cn.Query<dynamic>(sql);
}

var rows = query as IDictionary<string, object>;

foreach (var row in rows)
{
// Getting values and objects
}

但是转换总是返回 null。有什么线索吗?

最佳答案

您需要将各个行转换为 IDictionary<string, object> :

    const string sql = @"SELECT Name, Street FROM Contact";
IEnumerable<dynamic> results = null;

using (var cn = Connection)
{
results = cn.Query<dynamic>(sql);
}

foreach (var row in results)
{
var fields = row as IDictionary<string, object>;
// do something with fields["Name"] and fields["Street"]
}

关于c# - 转换动态 Dapper 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28713879/

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