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

后端 未结 7 2382
庸人自扰
庸人自扰 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);
    

提交回复
热议问题