none
Get and set property's value from a class with dynamic properties RRS feed

  • Question

  • Hi,

    I don't know how to explain very well, so let me put a example of what I'm trying to do.

    I have a dynamic query such as:

    SELECT NM_COMPANY AS Property1, PHONE_COMPANY AS Property2, EMAIL_COMPANY AS Property3, FL_ACTIVE_COMPANY AS Property4, DT_COMPANY AS Propery5 FROM TB_COMPANY --WHERE SomeClausure

    This kind of query is read from the database.. Also in the table of that query there's some columns informing the type of those properties used as alias in the query. In this example, I know that Property1 is a name, Property2 is a phone number and Property3 is a email (need to put mask after), Property4 is a flag that I need to convert in string after ("Yes" or "No") and Property5 is a date that I need to use something like '.ToShortDateString()'.

    I need to 'translate' all those properties to string in order to show those values to the user.

    So, to execute that query:

    public class GridDetails
    {
        public dynamic Property1 { get; set; }
        public dynamic Property2 { get; set; }
        public dynamic Property3 { get; set; }
        public dynamic Property4 { get; set; }
        public dynamic Property5 { get; set; }
        public dynamic Property6 { get; set; }
        public dynamic Property7 { get; set; }
        public dynamic Property8 { get; set; }
        public dynamic Property9 { get; set; }
        public dynamic Property10 { get; set; }
    }
    
    List<GridDetails> detailsList = DataContext.ExecuteQuery<GridDetails>(queryGrid).ToList();

    Ok, so I have a list with a lot of dynamic properties that I need to access in order to 'translate' their values: converting to string, putting mask, using .ToShortDateString()...

    How can I do that?

    Points:

    • Everything come from database, so everything is dynamic. I only need the class 'GridDetails' to execute the query using linq, that's why there are only dynamic properties;
    • I have only LINQ in this project


    Wednesday, July 15, 2015 7:07 PM

Answers

  • Hello,

    >> I know that Property1 is a name, Property2 is a phone number and Property3 is a email (need to put mask after), Property4 is a flag that I need to convert in string after ("Yes" or "No") and

    You will know that their mapped names between your defined dynamic class and the table, if so, you could use type cast approach to accomplish the cast, for example, for a date column, the dynamic property would know that it returns a datetime type value, so you could firstly cast it to a datetime type and then use the ToShortDateString() to convert it to string, here is the demo code:

    string sql = "select UserID Property1,UserName Property2,Flag Property3, Date Property4 from Users";
    
    
                        var result = db.ExecuteQuery<Test>(sql).ToList();
    
    
                        foreach (var r in result)
    
                        {
    
                               
    
                               //as you mentioned, you already know that the property4 would be mapped to a specific column in table, you could cast it to its database type
    
                            if (r.Property4 is DateTime)
    
                            {
    
                                string value = ((DateTime)(r.Property4)).ToShortDateString();
    
                                
    
                            }
    
                        }
    
    public class Test
    
        {
    
            public dynamic Property1 { get; set; }
    
            public dynamic Property2 { get; set; }
    
            public dynamic Property3 { get; set; }
    
            public dynamic Property4 { get; set; }
    
        }
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, July 16, 2015 2:01 AM
    Moderator