数据库内容和动态返回结果集在ASP.NET MVC [英] Database context and Return Dynamic Result Set in ASP.NET MVC
问题描述
在MVC 4和5 EF我要运行动态查询。
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:
字段1,字段2,字段3,字段4,字段5
Field 1, Field 2, Field 3, Field 4, Field 5
ROW1 ...
的Row2 ..
鸭preciate任何建议。
Appreciate any suggestion.
推荐答案
最后我提出使用由Mortalus和ExpandoObject对象建议TypeBuilder选项。它有小的性能开销现在。
Finally i made is using TypeBuilder option suggested by "Mortalus" and ExpandoObject object. It has little performance overhead right now.
从拿Mortalus的答案Typebuilder code,那么我做了code按照下述我的要求。
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.
这篇关于数据库内容和动态返回结果集在ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!