Search code examples
c#asp.net-mvcdapper

Dapper Querylist return type


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?


Solution

  • 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.