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.
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.
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.
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?
Your help is greatly appreciated!!
Thanks for your help Richard this is what my function looks like right now based on your suggestion for using DbDataRecord
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
in ASPX page
<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:
The result type 'System.Data.Common.DbDataRecord' may not be abstract and must include a default constructor
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?
If you already have an Entity type that matches your proc return type, use it as the type parameter.
Otherwise you could use an ADO.NET DataReader to build the list manually.
If you want to present the results in say a GridView then actually you're nearly there - with the binding and AutoGenerate columns, because ObjectQuery is an IEnumerable. I'm using ObjectQuery but you can interchange this with ExecuteStoreQuery - as they both return an IEnumerable
in the XAML
and you'll see the columns that are returned directly in the UI.
If you're not using WPF and you want a List of your elements then all you need to do is:
or to take your original method it would be:
If you do need to iterate over the fields; then the following will do the trick:
One of the many beauties of LINQ and Generics is that most of the time you can simply not worry too much about the actual datatypes until you eventually get to where you need to process them.
what about something like this: