Database context and Return Dynamic Result Set in ASP.NET MVC

asp.net-mvc-3, asp.net-mvc-4, c#, entity-framework

Solution

Finally i made is using TypeBuilder option suggested by "Mortalus" and ExpandoObject object. It has little performance overhead right now.

Take Typebuilder code from "Mortalus" answer then i made code according to my requirement as below.

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

foreach (var item in returndata)
  {
  IDictionary<string, object> expando = new ExpandoObject();
  foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(item))
     {
      var obj = propertyDescriptor.GetValue(item);
      expando.Add(propertyDescriptor.Name, obj);
     }
     expandolist.Add(new Dictionary<string, object>(expando));
  }

  return expandolist;

so now, I have "Dictionary" object from dynamic object. and using it you can work easily at design time rather then wait until runtime using "dynamic" object.

Problem

In MVC 4 and EF 5 i want to run dynamic query. ``` var returndata = Context.Database.SqlQuery(Type, strsql, null); ``` i don't know, how many fields it will return and name. Out of this result i want to make table structure that will display on view. Question : What should i passed as Type? my query return below result: Field 1, Field 2, Field 3, Field 4, Field 5 Row1... Row2.. Appreciate any suggestion.

Original source