none
dynamic serialization data to json RRS feed

  • Question

  • i get data use "select * from tab1", for some reason i can't list all columns in query, i only can use * to get all columns.

    i don't want to hold data use datatable, i intent to use entity framework way to hold data. For example,

    dynamic result = context.ExecuteStoreQuery<dynamic>("select *  from tab1");

    can i get the data and column name in runtime, and serialization them to json.

    Friday, August 30, 2013 5:27 AM

Answers

  • Hello,

    With my test, I found that it will have no properties if we use codes like:

    dynamic result = context.ExecuteStoreQuery<dynamic>("select *  from tab1");

    Because we do not know what is the type of result, we need to make sure the object type.

    As far as I know we can use reflection to create class type we need at runtime.

    Please have a look at the link below:

    http://msdn.microsoft.com/en-us/library/b8ytshk6.aspx

    Sample:

    http://www.mindfiresolutions.com/Create-Dynamic-Objects-at-runtime-using-Reflection-1029.php

    I look forward to hearing from you.

    Best Regards


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Andrea Pirlo Thursday, September 5, 2013 12:40 PM
    Tuesday, September 3, 2013 6:02 AM
    Moderator

All replies

  • any comment?
    Saturday, August 31, 2013 3:48 PM
  • Hello,

    Thank you for posting your question to this forum.

    From your description, you want to know how to serialize data to json dynamicly.

    If I have misunderstood, please let me know.

    As far as I know we usually use the namespace System.Runtime.Serialization.Json to serialize an object to json.

    So since the entity is an object, we could serialize it using System.Runtime.Serialization.Json, too.

    Please see the sample below:

    ObjectContext context = ((IObjectContextAdapter)db).ObjectContext;
                    List<Student> lst = context.ExecuteStoreQuery<Student>("select * from Students").ToList();
                    string szJson = "";
                    var result = from s in lst
                                 select new { s.StudentID, s.StudentName };
                    foreach (Student s in lst)
                    {
                        DataContractJsonSerializer json = new DataContractJsonSerializer(s.GetType());
                        //
                        using (MemoryStream stream = new MemoryStream())
                        {
                            json.WriteObject(stream, s);
                            szJson += Encoding.UTF8.GetString(stream.ToArray()) + "\n";
                        }
                    }
    

    And the result:

    My Students table data:

    I look forward to hearing from you.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 2, 2013 2:45 AM
    Moderator
  • I can't use ExecuteStoreQuery<Student>, because i can't use Student entity.

    My table structure will change at run time, that means studentNSex column in student table will reomoved at run time or some new columns will be added. so i just use dynamic result = context.ExecuteStoreQuery<dynamic>("select *  from tab1");

    and i intend to serialization  this table  use entity framework. or  is there an approach to create entity class--student at run time ?

    Monday, September 2, 2013 3:13 PM
  • Hello,

    With my test, I found that it will have no properties if we use codes like:

    dynamic result = context.ExecuteStoreQuery<dynamic>("select *  from tab1");

    Because we do not know what is the type of result, we need to make sure the object type.

    As far as I know we can use reflection to create class type we need at runtime.

    Please have a look at the link below:

    http://msdn.microsoft.com/en-us/library/b8ytshk6.aspx

    Sample:

    http://www.mindfiresolutions.com/Create-Dynamic-Objects-at-runtime-using-Reflection-1029.php

    I look forward to hearing from you.

    Best Regards


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Andrea Pirlo Thursday, September 5, 2013 12:40 PM
    Tuesday, September 3, 2013 6:02 AM
    Moderator
  • Thanks for your reply,  i have check the links, but i still don't know how to implement it in this case.
    Tuesday, September 3, 2013 10:54 AM