This is the code I have:

public class BusinessLayer
{
    public Employee[] GetAllEmployees() 
    {
        Employee[] employees;
        DataSet myDataSet = DataBaseLayer.GetAllEmployees();

      /// How can I convert the myDataSet to Employee List
      return employees;
   }     
}

Dani AI

Generated

Good question, and +1 to for asking about shapes. If you want your BLL to return domain objects instead of ADO.NET types (which makes testing and UI code cleaner), map the first table in the DataSet to a list. Use DataRow.Field<T>() so you do not blow up on DBNull and so casts are safe.

public sealed class Employee
{
    public int Id { get; set; }
    public string FirstName { get; set; } = "";
    public string LastName { get; set; } = "";
    public DateTime? HireDate { get; set; }
    // add other columns as needed
}

private static List<Employee> ToEmployees(DataSet ds)
{
    var list = new List<Employee>();
    if (ds == null || ds.Tables.Count == 0) return list;

    var t = ds.Tables[0];
    foreach (DataRow r in t.Rows)
    {
        list.Add(new Employee
        {
            Id = r.Field<int>("EmployeeId"),
            FirstName = r.Field<string>("FirstName"),
            LastName = r.Field<string>("LastName"),
            HireDate = r.Field<DateTime?>("HireDate")
        });
    }
    return list;
}

Then your BLL method stays tiny and returns exactly what the presentation layer needs:

public Employee[] GetAllEmployees()
{
    var ds = DataBaseLayer.GetAllEmployees();
    return ToEmployees(ds).ToArray();
}

If you prefer a concise style and are on .NET 3.5+, LINQ to DataSet works too:

var list = ds.Tables[0].AsEnumerable()
    .Select(r => new Employee {
        Id = r.Field<int>("EmployeeId"),
        FirstName = r.Field<string>("FirstName"),
        LastName = r.Field<string>("LastName"),
        HireDate = r.Field<DateTime?>("HireDate")
    })
    .ToList();

Notes:

  • Adjust column names to match your table.
  • Use nullable types (e.g., DateTime?) for optional columns.
  • Consider 's point: you could skip conversion and read directly from the DataTable. But returning Employee objects from DAL/BLL keeps ADO.NET details out of higher layers and usually pays off in maintainability.

Recommended Answers

All 3 Replies

More information is needed:

1) structure of Employee
2) structure/contents of the DataSet returned by: DataBaseLayer.GetAllEmployees();

More information is needed:

1) structure of Employee
2) structure/contents of the DataSet returned by: DataBaseLayer.GetAllEmployees();

I am learning a new concept of programming, the right one (data access layer, business logic layer, and presentation layer)

For DAL I did:

public class DataBaseLayer {
   public static DataSet GetAllEmployees() {
      /// Sql Connection
      /// Sql Command
      /// Sql Reader

      return empDataSet;
   }

}

And how I am doing on BLL (the code from the 1st post)

Yes, that's good that you are separating the code. However, in order to tell you how to convert a DataSet or rather your empDataSet to populate your Employee [] employees , I still need to know how each is defined.

For the "empDataSet", I assume that contains records from an Employee table or something, but I don't know what the table columns are...

For the Employee type, I assume it contains members that represent the DB columns from 'Employee' table, but I don't know what these fields are either...

I would like to mention though, that if your DAL is populating the DataSet with records that will match the structure of your Employee type, you might achieve the end result without any needed conversion at all between the DAL and the BLL because if your BLL expects a table containing columns with particular names, you can simply use the returned DataSet's Tables[0], which is a DataTable that can be accessed by column name such as: empDataSet.Tables[0].Rows[0]["Lastname"]; , which would give you the table's 1st row value for column "Lastname" if that is what it is called. Of course this code should be broken out to make it less cryptic, but I just wanted to demonstrate that you might not need to do any conversion from DataSet to a separate Employee [] type.


Example of breaking down the empDataSet.Tables[0].Rows[0]["Lastname"]; mention above:

// Retrieve reference to the Employee table from DataSet...
                DataTable dt = empDataSet.Tables[0];

                // Process each employee record in table...
                foreach (DataRow dr in dt.Rows)
                {
                    string lastName = (string)dr["Lastname"];
                    string firstName = (string)dr["Firstname"];
                }

So, if the your BLL's GetAllEmployees method expects to process employee records from the DAL, you can just reference those records from the DataTable as mentioned above, thus avoiding any need to perform any conversion to an Employee[]--unless you have a real need to do that.;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.