我如何获得一个DataSet作为在Entity Framework 5.0中的查询结果? [英] How can I get a DataSet as the result of a query in Entity Framework 5.0?

查看:166
本文介绍了我如何获得一个DataSet作为在Entity Framework 5.0中的查询结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否可能。我可能想为我要做的工作使用错误的工具。



假设我想要执行以下操作:



我执行一个名为GetTableName的函数,该函数返回要从中获取数据的SQL表的名称。



称为GetFields,它返回SQL表中一个或多个字段的名称。



我想能够写另一个函数来返回一个DataSet或一些其他通用对象)包含由GetTableName指定的表中具有由GetFields指定的字段的行



我知道我可以用旧的ADO .net接口,但是有一个合适的方法来做Entity Framework?



要强调,代码将不会在编译时知道

解决方案

由于EF需要复杂的类型映射,所以很可能写一个从ExpandoObject继承的类,然后你可以(有很多编码)映射到一个DataTable类或者只是使用。然后你基本上需要告诉EF以某种方式尝试映射到这些属性。
然而,我认为这将是一个漫长而艰辛的代码运行,重载并且基本上重写EF:s对象映射的核心功能。



可以看到,在这个例子中,作家动态创建模型,硬仍然存在;基于数据自动生成属性的部分:
http://www.markzhou.com/blog/post/2011/06/02/Use-dynamic-type-in​​-Entity- Framework-41-SqlQuery() - method.aspx



如果您需要DataTable,我真的推荐您使用ADO.NET。


I'm not sure if this is even possible. I might be trying to use the wrong tool for the job I'm trying to do.

Suppose that I want to do the following:

I execute a function called "GetTableName" that returns the name of a SQL table I want to get data from.

I execute another function called "GetFields" that returns a the names of one or more fields in the SQL table.

I would like to be able to write another function that would return a DataSet (or some other generic object) containing rows from the table specified by "GetTableName" with the fields specified by "GetFields"

I know that I can do this with the old ADO.net interface, but is there an appropriate way to do this with Entity Framework?

To emphasize, the code will not know at compile time what the shape of the data returned from SQL will be.

解决方案

Since EF requires complex types to map onto, it's probably possible to write a class inheriting from ExpandoObject which you would then be able (with alot of coding) to map onto a DataTable class or just use as is. Then you'd basically need to tell EF to somehow try map to those properties. However, I think that would be a long and winding journey of code, overloads and basicly rewriting the core functionality of EF:s object mapping.

As you can see in this example, where the writer dynamicly creates the model, the hard still remains; the part where you automaticly generate the properties based on the data: http://www.markzhou.com/blog/post/2011/06/02/Use-dynamic-type-in-Entity-Framework-41-SqlQuery()-method.aspx

I'd really recommend you go with ADO.NET if you need DataTable.

这篇关于我如何获得一个DataSet作为在Entity Framework 5.0中的查询结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆