有没有通过字符串或索引来访问在一个小巧玲珑的FastExpando列的方式吗? [英] Is there a way to access the columns in a Dapper FastExpando via string or index?
问题描述
我在小巧玲珑FastExpando
对象拉动和希望能够在运行时,而不是在设计动态引用的列名/编译时间。所以,我希望能够做到以下几点:
I am pulling in a Dapper FastExpando
object and want to be able to reference the column names dynamically at run time rather than at design/compile time. So I want to be able to do the following:
var testdata = conn.Query("select * from Ride Where RiderNum = 21457");
我希望能够做到以下几点:
I want to be able to do the following:
foreach( var row in testdata) {
var Value = row["PropertyA"];
}
我知道我可以做的:
I understand that I can do:
var Value = row.PropertyA;
但我不能这样做,因为属性的名称,我要去赢得了需要 T为直到运行时才知道。
but I can't do that since the name of the property i'm going to need won't be known until runtime.
从的这太问题不起作用。我仍然得到同样的目标调用
例外。所以...
The answer from this SO Question doesn't work. I still get the same Target Invocation
exception. So...
有没有办法做我想做一个小巧玲珑的FastExpando办?
推荐答案
当然,这实际上比这种方式更容易:
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");
这篇关于有没有通过字符串或索引来访问在一个小巧玲珑的FastExpando列的方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!