返回自定义对象<名单T>从实体框架,并分配到对象数据源 [英] Return Custom Object <List T> from Entity framework and assign to Object Data Source

查看:186
本文介绍了返回自定义对象<名单T>从实体框架,并分配到对象数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些指导的问题,我使用实体框架4.0,我有一个DAL和BLL和我在页面上绑定的ObjectDataSource。

I need some guidance with an issue, I am using Entity Framework 4.0, I have a DAL and BLL and am binding to ObjectDataSource on the page.

我不得不使用 PIVOT 和动态SQL返回多个实体数据的方式,我写了一个存储过程想。现在我想弄清楚我如何能得到实体框架返回,我可以绑定到我的 ObjectDataSource控件页面上的自定义对象,我需要使用自定义对象或由于存储过程的动态对象可以返回任意数量的列,所以我不能用一个强类型的类或实体的,我必须还能够与 ObjectDataSource控件。

I had to write a stored procedure using PIVOT and dynamic SQL to return the data from multiple entities the way I want. Now I am trying to figure out how can I get Entity Framework to return a custom object that I can bind to my ObjectDataSource on the page, I NEED to use a custom object or a dynamic object since the stored procedure can return any number of columns so I can't use a strongly typed class or entity and I need to be also able to bind this with an ObjectDataSource.

有人能指出一个好办法做到这一点,如何界定我的功能?随着一些代码示例,请

Can someone point out a good way to do this and how to define my function? With some code examples please.

我读,我应该尝试使用列表< T> 用于返回一个对象因为EF不支持返回数据表/数据集,我有以下为止,但我知道这是不正确的。

I read that I should try to use List<T> for returning an object since EF does not support returning datatables/datasets, I have the following so far but I know this isn't correct.

我还没有与仿制药的工作很多,如果你可以指出如何做到这一点我敢肯定,这将让很多人有所帮助。请提供功能代码示例和如何绑定 ObjectDataSource控件来返回的对象?

I have not worked with generics much, if you could point out how to do this I'm sure this would be helpful for a lot of people. Please provide code examples for the function and how to bind ObjectDataSource to the return object?

您的帮助感激!

感谢您的帮助理查德这就是我的函数貌似现在根据您的建议使用DbDataRecord

C#功能/ p>

C# function for ObjectDataSource in DAL

public List<DbDataRecord> GetData(int product_id)
{
    List<DbDataRecord> availableProducts = new List<DbDataRecord>();

    var groupData = context.ExecuteStoreQuery<DbDataRecord>("exec 
  spGetProducts @ProductID={0}", product_id);

    availableProducts = groupData.ToList<DbDataRecord>();

    return availableProducts;
}



ObjectDataSource控件在ASPX页

<asp:ObjectDataSource ID="ODSProductAvailability" runat="server"
        TypeName="Project.BLL.ProductBL" 
        SelectMethod="GetData"  >
     <SelectParameters>
        <asp:SessionParameter Name="product_id" SessionField="ProductID" />
     </SelectParameters>
</asp:ObjectDataSource>



现在,当我访问该页面,我得到这个错误:

Right now I'm getting this error when I access the page:

的结果类型System.Data.Common.DbDataRecord'可能不是抽象的,必须有一个默认的构造

这是因为 ExecuteStoreQuery 预计将被定义的类或实体? ?我如何才能创建一个基于存储过程的结果的对象,并为其分配一个

Is this because the ExecuteStoreQuery expects to be defined class or entity? How can I just create an object based on the stored procedure results and assign it that?

推荐答案

什么是这样的:


what about something like this:

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    string myQuery = @"SELECT p.ProductID, p.Name FROM 
        AdventureWorksEntities.Products as p";

    foreach (DbDataRecord rec in new ObjectQuery<DbDataRecord>(myQuery, context))
    {
        Console.WriteLine("ID {0}; Name {1}", rec[0], rec[1]);
    }
}

这篇关于返回自定义对象&LT;名单T&GT;从实体框架,并分配到对象数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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