Dapper Querylist return type

Viewed 589

My code looks like this;

 using (IDbConnection cnn = new SqlConnection(GetConnectionString()))
{
    var result = cnn.QuerySingle(sql, parameters);

    return result;
}

Its inside a method inside a class but that shouldn't matter. QuerySingle is said to return a dynamic type, but I have never worked with this before. When I debug this code, what it returns is something like this;

{{DapperRow, Tcode = 'eeeee', Hashedpw = 'NF886jMDl5imyMj0ThDIxA==', Salt = 'Z+HHq6Rt60FPnAf80Lg4Dg=='}}

How should I read this? Are they key-value pairs because the logic behind that doesn't work, it doesn't work like a list or an array. Im completly lost and have no idea what this returns. I only need the values, not the dapperrow, tcode, hashedpw and salt. So bassicly, the return I would need is

['eeeee', 'NF886jMDl5imyMj0ThDIxA==','Z+HHq6Rt60FPnAf80Lg4Dg=='] 

Can anyone help me out?

2 Answers

Currently you are using weak typed QuerySingle(), which will always return key value pair result. If you want to retrieve only values you should use strongly typed QuerySingle() like this.

var result= connection.QuerySingle<TYPE>(sql, parameters);

EDIT: If i got you right

you can define class for example

public class SomeClass 
{
  public string Salt {get;set;}
  public string TCode {get;set;}
  public string Hashedpw {get;set;}
}

And you can use this in the code like this QuerySingle<SomeClass> if you are returning multiple result in that case obviously you have to use like this QuerySingle<List<SomeClass>>().

Hope this will help you.

Checkout this link for help on QuerySingle

If you create another class, something like this ...

public class TestClass
{
    public string Tcode { get; set; }
    public string Hashedpw { get; set; }
    public string Salt { get; set; }
}

... and changed your QuerySingle call to something like this ...

var result = cnn.QuerySingle<TestClass>(sql, parameters);

... a call like ...

Debug.WriteLine(result.Tcode);

... would result in eeeee comming out of your Debug window

Does that help?

Related