none
How to use DynamicDictionary Class for List (Collection) of Data? RRS feed

  • Question

  • I'm using entity framework but when I execute custom query that time I don't want to create Hard class to carry collection of data.

    I saw some link for DynamicDictionary Class and use but data was not populated in List<DynamicDictionary> collection.

    So please help me for that...

    How can I use DynamicDictionary class with List<T>?

    Thanks,

    Chetan

    Thursday, May 15, 2014 5:18 PM

Answers

  • Hello,

    I do not think it is possible, the DynamicDictionary needs us firstly set the properties as:

    dynamic person = new DynamicDictionary();
    
    // Adding new dynamic properties.  
    
    // The TrySetMember method is called.
    
    person.FirstName = "Ellen";
    
    person.LastName = "Adams";
    

    In the LINQQuery, there is no way to do this because we cannot declare a dynamic type instance in the query.

    These properties are stored with the instance rather than the type. So when calling with the DynamicDictionary, we actually does not set any property in it, of course it will return no data.

    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.

    Friday, May 16, 2014 10:35 AM
    Moderator

All replies

  • I write following code to achieve List of data from Entity FW LINQ Query.

    private static TFAT_WEBERPEntities ctx = new TFAT_WEBERPEntities();
    
    [HttpPost]
            public ActionResult GetQueryResult(FormCollection fc)
            {
                var ListDyn = ctx.Database.SqlQuery<DynamicDictionary>(fc["txtQuery"].ToString()).ToList();
                return Content(ListDyn.ToList().ToString());
            }
    
    
            // The class derived from DynamicObject. 
            internal class DynamicDictionary : DynamicObject
            {
                // The inner dictionary.
                Dictionary<string, object> dictionary = new Dictionary<string, object>();
    
                // This property returns the number of elements 
                // in the inner dictionary. 
                public int Count
                {
                    get
                    {
                        return dictionary.Count;
                    }
                }
    
                // If you try to get a value of a property  
                // not defined in the class, this method is called. 
                public override bool TryGetMember(GetMemberBinder binder, out object result)
                {
                    // Converting the property name to lowercase 
                    // so that property names become case-insensitive. 
                    string name = binder.Name.ToLower();
    
                    // If the property name is found in a dictionary, 
                    // set the result parameter to the property value and return true. 
                    // Otherwise, return false. 
                    return dictionary.TryGetValue(name, out result);
                }
    
                // If you try to set a value of a property that is 
                // not defined in the class, this method is called. 
                public override bool TrySetMember(SetMemberBinder binder, object value)
                {
                    // Converting the property name to lowercase 
                    // so that property names become case-insensitive.
                    dictionary[binder.Name.ToLower()] = value;
    
                    // You can always add a value to a dictionary, 
                    // so this method always returns true. 
                    return true;
                }
            }

    There is no any data in dictionary object.

    So please help me its urgent.


    Thannks, Chetan

    Friday, May 16, 2014 6:24 AM
  • Hello,

    I do not think it is possible, the DynamicDictionary needs us firstly set the properties as:

    dynamic person = new DynamicDictionary();
    
    // Adding new dynamic properties.  
    
    // The TrySetMember method is called.
    
    person.FirstName = "Ellen";
    
    person.LastName = "Adams";
    

    In the LINQQuery, there is no way to do this because we cannot declare a dynamic type instance in the query.

    These properties are stored with the instance rather than the type. So when calling with the DynamicDictionary, we actually does not set any property in it, of course it will return no data.

    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.

    Friday, May 16, 2014 10:35 AM
    Moderator
  • I usually just use a List<object>

    Dictionary<string,List<object>> dict = new Dictionary<string,List<object>>();


    jdweng

    Friday, May 16, 2014 11:42 AM