I have 4 DataTables that I am trying to join, but can't figure out how to efficiently do so.
I got the first two tables to join, creating a 3rd object of apptDetails
which is an IEnumerable
of DataRows. I am having trouble getting it back to a DataTable so I can do more joins on it though. I am getting an error on apptDetails.CopyToDataTable()
of: 'IEnumerable' does not contain a definition for 'CopyToDataTable' and no accessible extension method 'CopyToDataTable' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)
DataTable customer = ETL.ParseTable("customer");
DataTable appointments = ETL.ParseTable("appointments");
IEnumerable apptDetails = from t1 in customer.AsEnumerable()
join t2 in appointments.AsEnumerable() on Convert.ToInt32(t1["customerId"]) equals Convert.ToInt32(t2["customerId"])
into tableGroup
select new
{
customerId = t1["customerId"],
TotalAppointments = tableGroup.Count(),
appointment_missed = Convert.ToInt32(t1["MissedAppt"]),
appointment_show_rate = (
tableGroup.Count()>0 ?
Math.Round((1 - ((double)Convert.ToInt32(t1["MissedAppt"]) / (double)tableGroup.Count())),2)
: 0
)
};
DataTable dt = apptDetails.CopyToDataTable();
I had originally just used var apptDetails
, but it looked like I needed more typecasting, so I tried things like the following:
IEnumerable<DataRow> apptDetails
IEnumerable<EnumerableRowCollection> apptDetails
as well as:
DataTable dt = apptDetails.CopyToDataTable<DataRow>();
DataTable dt = apptDetails.CopyToDataTable<EnumerableRowCollection>();
I need to join the customer and appointments tables, and then tack on the new columns as well into a single flat table. What am I missing about how I am doing it, or is there a better way of doing it?
Performance is a factor as we are talking 20,000 customers and 80,000 appointments, plus there will be 2-3 more tables to join after this so I would like to learn the "Right" way of doing this using Linq to it's fullest.