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

前端 未结 8 1690
说谎
说谎 2020-12-14 13:42

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 fie

8条回答
  •  天涯浪人
    2020-12-14 14:00

    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> expandolist = new List>();
    
    foreach (var item in returndata)
      {
      IDictionary expando = new ExpandoObject();
      foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(item))
         {
          var obj = propertyDescriptor.GetValue(item);
          expando.Add(propertyDescriptor.Name, obj);
         }
         expandolist.Add(new Dictionary(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.

提交回复
热议问题