Is there a way to access the columns in a Dapper FastExpando via string or index?

时光毁灭记忆、已成空白 提交于 2019-11-30 04:41:36

Sure, it is actually way easier than that:

var sql = "select 1 A, 'two' B";
var row = (IDictionary<string, object>)connection.Query(sql).First();
row["A"].IsEqualTo(1);
row["B"].IsEqualTo("two");

Regarding the portion of the title "or index?" - I needed to access results by index since the column names being returned changed sometimes, so you can use a variation of Sam Saffron's answer like this:

var sql = "select 1, 'two'";
var row = (IDictionary<string, object>)connection.Query(sql).First();
row.Values.ElementAt(0).IsEqualTo(1);
row.Values.ElementAt(1).IsEqualTo("two");

There a simple way to access fields direct below sample

string strConexao = WebConfigurationManager.ConnectionStrings["connection"].ConnectionString;

conexaoBD = new SqlConnection(strConexao);
conexaoBD.Open();

var result = conexaoBD.Query("Select Field1,Field2 from Table").First();

//access field value result.Field1 
//access field value result.Field2

if (result.Field1 == "abc"){ dosomething}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!