-2

Is it possible to convert an IDictionary into a class?

My IDictionary looks like this

{{DapperRow,   
 Date = '9/25/2014 12:00:00 AM',    
 UserId = '123456',    
 User = 'Timothy'    
}}

my class

public class MyClass
{
    public DateTime Date {get; set;}
    public string User {get; set;}
    public int UserId {get; set;}
    public virtual someModel {get; set;}    
}
CodeMan03
  • 570
  • 3
  • 18
  • 43
  • Why do you even have a dictionary? Can you show the code for how you get that? – DavidG Jan 16 '19 at 17:25
  • 2
    How do you retrieve this? Why not use eg `connection.Query(..)` ? – Panagiotis Kanavos Jan 16 '19 at 17:26
  • Did you use `connection.Query()` perhaps? Dapper's job is to load results *and* generate the DTOs you passed as a type parameter. When you *don't* specify a specific type, it will generate an dynamic object that's derived from [DynamicObject](https://learn.microsoft.com/en-us/dotnet/api/system.dynamic.dynamicobject?view=netframework-4.7.2). You can access individual properties directly if you use `dynamic` as the variable type. – Panagiotis Kanavos Jan 16 '19 at 17:30
  • Duplicate of https://stackoverflow.com/questions/4943817/mapping-object-to-dictionary-and-vice-versa/4944547 – Manoj Choudhari Jan 16 '19 at 17:30
  • @ManojChoudhari it's not. This is a question about Dapper, not dictionaries in general. – Panagiotis Kanavos Jan 16 '19 at 17:32
  • The reason you see an `IDictionary` in *some* Dapper questions is that custom dynamic objects typically use a Dictionary internally which they also expose through `IDictionary`. Check for example [ExpandoObject](https://learn.microsoft.com/en-us/dotnet/api/system.dynamic.expandoobject?view=netframework-4.7.2). Casting the dynamic object to a dictionary allows reading the column names – Panagiotis Kanavos Jan 16 '19 at 17:34
  • @PanagiotisKanavos Correct that is exactly how I got this Idictionary – CodeMan03 Jan 16 '19 at 18:13
  • @PanagiotisKanavos Also I cant explicity use Query as I need to use this same query for at least 30 different classes. So I gather the data using then I need to map it to a it's respective class after the SQL returns its results.. – CodeMan03 Jan 16 '19 at 18:19
  • @CodeMan03 post your code. If you need that conversion, you already know the correct type and so you *can* use `Query`. The query you refer to is a sql string after all. Nothing prevents you from using it with different type arguments. If you mean you have a *function* that calls `Query` which you want to reuse, make it generic as well – Panagiotis Kanavos Jan 17 '19 at 07:57
  • @CodeMan03 with a `dynamic` object you can access any of the existing members the same way you'd access the members of another class. You can write `result.Date` or `result.User` to get the property values – Panagiotis Kanavos Jan 17 '19 at 07:59

1 Answers1

0

To convert the dictionary you provided to the class you provided, you can just add a constructor to the model class to read the dictionary values.

public class MyClass
{
    public DateTime Date { get; set; }
    public string User { get; set; }
    public int UserId { get; set; }

    public MyClass(IDictionary<string, object> data)
    {
        Date = DateTime.Parse(data["Date"].ToString());
        User = data["User"].ToString();
        UserId = int.Parse(data["UserId"].ToString());
    }
}

But Panagiotis is right, in this situation you should be taking advantage of dapper to automagically map data to your models for you. Their readme is incredibly helpful if you need to figure out how it works or how to do complex mappings.

pkatsourakis
  • 1,024
  • 7
  • 20