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

后端 未结 7 2361
庸人自扰
庸人自扰 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条回答
  • 2020-12-07 22:53

    If you are using > .net 4.7 or netstandard2 you can use value tuples. the code is nice and terse and there is no use of dynamics.

    var sql = "SELECT UniqueString, Id  FROM Table";
    var dict = conn.Query<(string UniqueString, int Id)>(sql)
               .ToDictionary(t => t.UniqueString,t => t.Id);
    
    0 讨论(0)
  • 2020-12-07 22:58

    Works also without an additional class:

    var myDictionary = conn.Query<string, int, KeyValuePair<string,int>>(sql, (s,i) => new KeyValuePair<string, int>(s,i))
        .ToDictionary(kv => kv.Key, kv => kv.Value);
    

    NOTE: When using Dapper.NET 3.5 version, the Query method that takes the first, second and return types requires you specify more parameters, as the .NET 4.0 and .NET 4.5 versions take advantage of optional arguments.

    In this case, the following code should work:

    string splitOn = "TheNameOfTheValueColumn";
    var myDictionary = conn.Query<string, int, KeyValuePair<string,int>>(sql, (s,i) => new KeyValuePair<string, int>(s,i), null, null, false, splitOn, null, null)
            .ToDictionary(kv => kv.Key, kv => kv.Value);
    

    Most of the arguments will revert to a default, but splitOn is required, as it will otherwise default to a value of 'id'.

    For a query that returns two columns, 'ID' and 'Description', splitOn should be set to 'Description'.

    0 讨论(0)
  • 2020-12-07 23:04

    I'm not sure if what you're trying to do is possible. If you define a class to map the query to this becomes far more trivial:

    public class MyRow
    {
        public int Id { get; set; }
        public string UniqueString { get; set; }
    }
    

    Then, you would just do this:

    var sql = "SELECT UniqueString, ID  FROM Table";
    var myDictionary = conn.Query<MyRow>(sql).ToDictionary(row => row.UniqueString, row => row.Id);
    
    0 讨论(0)
  • 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<IDictionary<string, object>>()
            .Select(it => it.ToDictionary(it => it.Key, it => it.Value));
    
    0 讨论(0)
  • 2020-12-07 23:14

    There's various ways already shown; personally I'd just use the non-generic api:

    var dict = conn.Query(sql, args).ToDictionary(
        row => (string)row.UniqueString,
        row => (int)row.Id);
    
    0 讨论(0)
  • 2020-12-07 23:15
    string strSql = "SELECT DISTINCT TableID AS [Key],TableName AS [Value] FROM dbo.TS_TStuctMaster";
    Dictionary<string,string> dicts = sqlConnection.Query<KeyValuePair<string,string>>(strSql).ToDictionary(pair => pair.Key, pair => pair.Value);
    

    You can use aliases and strong types.

    Aliases are the key points, which match the attributes of KeyValuePair type Key and Value.

    It works under strong typing and runs well.

    I don't like dynamic type. It brings disaster in certain situations. Moreover, the boxing and unboxing brings performance loss.

    0 讨论(0)
提交回复
热议问题