0

PetaPoco has this method :

public List<T> Fetch<T>(string sql, params object[] args) 
    {
        return Query<T>(sql, args).ToList();
    }

I want to create a method like this :

        public Dictionary<Guid, T> FetchDict<T>(string sql, params object[] args)
    {
//What goes here ?
        return Query<T>(sql, args).ToDictionary(x => ???, x => x);
    }

In the stub above, it should use reflection & use the PrimaryKey attribute and set that field as key. How can I do this ?

MD Luffy
  • 536
  • 6
  • 18

1 Answers1

0

Figured it out :

public Dictionary<K, T> FetchDict<K,T>(string sql, params object[] args)
    {   
        Type typ = typeof(T);
        var primKey = TableInfo.FromPoco(typ).PrimaryKey;
        var prop = typ.GetProperty(primKey);
        return Query<T>(sql, args).ToDictionary(x => (K)prop.GetValue(x), x => x);
    }
MD Luffy
  • 536
  • 6
  • 18