Is there a way to access the columns in a Dapper FastExpando via string or index?
Solution 1
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");
Solution 2
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");
Solution 3
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}
Related videos on Youtube
Jay Stevens
The first computers I programmed on were an IBM 8088 and then a Color Apple 2. I graduated in 1986 to a Mac-Plus so that I could record digital sequenced music. (What's the most powerful computer in the world? The one people use.) ... then I upgraded to dual floppy drives (Chuck Yeager Flight Simulator required it), then upgraded to a 40MB HD (all my MIDI programming demanded it), but then I was seduced by the PC-Master Race. (What's the most powerful computer in the world? The one that has software to run on it.) In 1990, I discovered SAS, SPSS and Statistics. So while I am .NET/Typescript/HTML/PWA geek architect; I am also a SAS-Ninja-Shadow-Warrior, machine learning, analytics, jedi master and data wrangler as well.
Updated on November 06, 2020Comments
-
Jay Stevens over 3 years
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;
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...Is there any way to do what I want to do with a Dapper FastExpando?
-
jbtule about 13 yearsWhile the chosen answer wouldn't work for "this SO Question", there was an answer on that question that would. stackoverflow.com/questions/4939508/…
-
-
crush over 9 yearsI find myself always using
FirstOrDefault()
and still having to check if row is null first before trying to access the row index. Is there a way to try and fetch a column value or a specific row, or return a default value if it doesn't exist?