Aravind Ranganathan
asked on
Multiple Join on Linq not working
i have an scenario where i have to compare live data from the database to the data in the customer file that we get every night and send only the changes between the two.
i am trying to create a join in LINQ where i need to join two columns, customer and status to that of the file.
if its the same customer and same status but all the other columns are different then write it as changed customer but if its the same customer and different status then i would like to update that customer record to a 4 which means removed customer. this is my linq code i am not sure how to do a join on multiple columns and how to do an update.
i would like to join customers and status on the below code
this is the linq i would like to update status to a 4
i am doing this after but this creates duplicates because instead of updating the status to 4 we now have another record.
i am trying to create a join in LINQ where i need to join two columns, customer and status to that of the file.
if its the same customer and same status but all the other columns are different then write it as changed customer but if its the same customer and different status then i would like to update that customer record to a 4 which means removed customer. this is my linq code i am not sure how to do a join on multiple columns and how to do an update.
i would like to join customers and status on the below code
DataTable tblSamebutotherchanges = (from r in FirstDataTable.AsEnumerable()
join s in SecondDataTable.AsEnumerable()
on r.Field<string>("Customer") equals s.Field<string>("Customer")
where r.Field<string>("STATUS").Trim() == s.Field<string>("STATUS").Trim() || r.Field<string>("Name").Trim() != s.Field<string>("Name").Trim() || r.Field<string>("ADDRESS").ToUpper().Trim() != s.Field<string>("ADDRESS").ToUpper().Trim() || r.Field<string>("ADDRESS 2").Trim() != s.Field<string>("ADDRESS 2").Trim() || r.Field<string>("CITY").ToUpper().Trim() != s.Field<string>("CITY").ToUpper().Trim() || r.Field<string>("STATE").Trim() != s.Field<string>("STATE").Trim() || r.Field<string>("ZIP CODE").Substring(0, 4).Trim() != s.Field<string>("ZIP CODE").Substring(0, 4).Trim() || r.Field<string>("PARENT").Trim() != s.Field<string>("PARENT").Trim() || r.Field<string>("TELEPHONE").Trim() != s.Field<string>("TELEPHONE").Trim() || r.Field<string>("FAX").Trim() != s.Field<string>("FAX").Trim() || r.Field<string>("TERMS OF PAYMENT").Trim() != s.Field<string>("TERMS OF PAYMENT").Trim() || r.Field<string>("COUNTRY").Trim() != s.Field<string>("COUNTRY").Trim() || r.Field<string>("CURRENCY").Trim() != s.Field<string>("CURRENCY").Trim() || r.Field<string>("LONG").Trim() != s.Field<string>("LONG").Trim() || r.Field<string>("LAT").Trim() != s.Field<string>("LAT").Trim() || r.Field<string>("TAXID").Trim() != s.Field<string>("TAXID").Trim()
select r).CopyToDataTable();
this is the linq i would like to update status to a 4
DataTable RemoveCustomers = (from r in FirstDataTable.AsEnumerable()
join s in SecondDataTable.AsEnumerable()
on r.Field<string>("Customer") equals s.Field<string>("Customer")
where r.Field<string>("STATUS").Trim() != s.Field<string>("STATUS").Trim()
select r).CopyToDataTable();
i am doing this after but this creates duplicates because instead of updating the status to 4 we now have another record.
foreach (DataRow dr in RemoveCustomers.Rows)
{
// string cust = dr["Customer"].ToString();
tblDiff.Rows.Add(dr["CUSTOMER"].ToString().Trim(), replaceapostrophe(dr["NAME"].ToString().Trim()), replaceapostrophe(dr["ADDRESS"].ToString().Trim()), replaceapostrophe(dr["ADDRESS 2"].ToString().Trim()), replaceapostrophe(dr["CITY"].ToString().Trim()), dr["STATE"].ToString().Trim(), dr["ZIP CODE"].ToString().Trim(), dr["PARENT"].ToString().Trim(), dr["TELEPHONE"].ToString().Trim(), dr["FAX"].ToString().Trim(), dr["TERMS OF PAYMENT"].ToString().Trim(), dr["COUNTRY"].ToString().Trim(), dr["CURRENCY"].ToString().Trim(), "4", dr["LONG"].ToString().Trim(), dr["LAT"].ToString().Trim(), dr["TAXID"].ToString().Trim());
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.