mardi 28 avril 2015

Match Object with Dataset.Datatable.Column and loop the data inside [duplicate]

This question already has an answer here:

I made a matching system where a user could upload .CSV files and match the data with our data objects. I have the matching system down, what I need help with is getting the data out of the files and matching them to the objects. I am saving the files to a DataSet one file per DataTable, at first I thought I would loop the objects and the DataSet to add the data.

public List<Customer> BuildCustomerList(DataSet ds, Customer oCustomer)
    {
        List<Customer> lCustomers = new List<Customer>();
        foreach (PropertyInfo p in typeof(Customer).GetProperties())
        {
            Customer oC = new Customer();
            foreach(DataRow dr in ds.Tables[0].Rows)
            { 
                foreach(DataColumn dc in ds.Tables[0].Columns)
                {
                    if(p.Name == dc.ColumnName)
                    {
                        p.SetValue(oC, dr[dc].ToString());
                    }
                }  
            }
            lCustomers.Add(oC);
        }
        return lCustomers;
    }

But this will not work, and if I start with loping the DataSet first it will also not work. I am looking for a solution to loop the Data and save them to a list of objects.





Aucun commentaire:

Enregistrer un commentaire