Mapping to Dictionary collection in PetaPoco?
Asked Answered
O

2

5

way to map the following to a Dictionary??

Sql sql = new Sql()
.Append("SELECT Count(*) ")
.Append("FROM Custumers")
.Append("WHERE CustomerId = @0", Id)

var result = database.Fetch<Dictionary<int,DateTime>>(sql);

I cannot use List as DateTime is also thr.

Overexcite answered 30/5, 2013 at 15:53 Comment(0)
C
7

Petapoco always return a List<T>, but you can convert the List to a Dictionary afterwards:

var result = database
    .Fetch<Pair<int,DateTime>>(sql)
    .ToDictionary(i => i.ID, i => i.Date);
Cristinecristiona answered 30/5, 2013 at 20:29 Comment(2)
What is Pair in this case? KeyValuePair does not workTater
For anyone else that is reading this and is wondering what a Pair is, it looks like it's a generic class that was inspired by the answer at https://mcmap.net/q/1143920/-mapping-to-collection-in-petapocoShowiness
A
6

With NPoco you can write this: it will use the first 2 columns

var result = database.Dictionary<int, DateTime>(sql);

or use what @Eduardo said.

Andriaandriana answered 1/6, 2013 at 5:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.