Specify field names when mapping List in Dapper

c# dapper

Question

I have been getting results out of Dapper as a Dictionary:

  using (var multi = cnn.QueryMultiple("dbo.[s_Dashboard_stats]", 
     commandType: CommandType.StoredProcedure))
  {
    var recentHits =
        multi.Read().ToDictionary(x => (string)x.URL, x => (double)x.MinsAgo);
  }

This was working fine but a change in the data has led (correctly) to duplicate key values. To get around this I thought I'd change the dictionary to a List instead:

var PopularHits =
    multi.Read<KeyValuePair<string, double>>().ToList<KeyValuePair<string, double>>();

The list ends up with items but the values of the KVP are null, how do I project the values into the KeyValuePair objects? I can't seem to get the lambda syntax correct.....

Accepted Answer

multi.Read()
    .Select(x => new KeyValuePair<string, double>((string) x.URL, (double) x.MinsAgo)
    .ToList();



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why