Return Custom Object from Entity framework and assign to Object Data Source

前端 未结 3 1214
暗喜
暗喜 2021-01-18 02:02

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 pr

3条回答
  •  深忆病人
    2021-01-18 02:58

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

    public List GetData(int product_id) where T : class 
    {
    
        List myList = new List(); 
    
        var groupData = context.ExecuteStoreQuery("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 list = new List();
        if (reader.HasRows)
        {
            while (reader.Read())
            {
                list.Add(new ProcType(){Property1 = reader.GetInt32(0), Property1 = reader.GetString(1));
            }
        }
        reader.Close();
    
        return list;
    }
    

提交回复
热议问题