How to map to a Dictionary object from database results using Dapper Dot Net?

后端 未结 7 2396
庸人自扰
庸人自扰 2020-12-07 22:38

If I have a simple query such as:

string sql = \"SELECT UniqueString, ID  FROM Table\";

and I want to map it to a dictionary object such as

7条回答
  •  萌比男神i
    2020-12-07 23:07

    For the table of which you do not know the structure at runtime

        using var db = new SqlConnection(_connectionString);
        var sql = $"Select * From {tableName} Order By {primaryKey}";
    
        var result = await db.QueryAsync(sql); 
        return result
            .Cast>()
            .Select(it => it.ToDictionary(it => it.Key, it => it.Value));
    

提交回复
热议问题