Return Custom Object <List T> from Entity framework and assign to Object Data Source

拥有回忆 提交于 2019-12-01 16:05:52

If you already have an Entity type that matches your proc return type, use it as the type parameter.

public List<MyEntity> GetData<MyEntity>(int product_id) where T : class 
{

    List<MyEntity> myList = new List<MyEntity>(); 

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

    return myList;
}

Otherwise you could use an ADO.NET DataReader to build the list manually.

using (SqlConnection connection = new SqlConnection("your connection string"))
{
    SqlCommand command = new SqlCommand(
      "exec spGetProductsByGroup @ProductID",
      connection);
    command.Parameters.Add(product_id);

    connection.Open();

    SqlDataReader reader = command.ExecuteReader();

    List<ProcType> list = new List<ProcType>();
    if (reader.HasRows)
    {
        while (reader.Read())
        {
            list.Add(new ProcType(){Property1 = reader.GetInt32(0), Property1 = reader.GetString(1));
        }
    }
    reader.Close();

    return list;
}

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]);
    }
}

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

string myQuery = @"SELECT p.Name,p.VatNumber FROM MyEntities.Users as p";

ProductList.ItemsSource = new ObjectQuery<DbDataRecord>(myQuery, context);

in the XAML

<DataGrid AutoGenerateColumns="True" x:Name="ProductList"/>

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:

var myList =  new ObjectQuery<DbDataRecord>(myQuery, context).ToList();

or to take your original method it would be:

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

If you do need to iterate over the fields; then the following will do the trick:

foreach (var rec in context.ExecuteStoreQuery<DbDataRecord>("exec spGetProductsByGroup @ProductID={0}", product_id))
    {
    for (var ri = 0; ri < rec.FieldCount;ri++)
    {
        Console.WriteLine(rec.GetDataTypeName(ri)
                          +"   " + rec.GetName(ri)
                          +" = " + rec.GetValue(ri));
        }
    }

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!