var customer= from cust in customerData
select new Customer
{
CustomerID = cust["id"],
Name = cust["Name"],
LastVisit = cust["visit"],
PurchashedAmount = cust["amount"],
Tagged = cust["tagged"]
Code = cust["code"]
}
The rows looks like this
Name LastVisit PurchasedAmount Tagged Code CustomerID
------ --------- -------------- ------ ----- -----
Joshua 07-Jan-09 Yes chiJan01 A001
Joshua 10000
The 2nd row belongs to first row just that the other columns are empty.How can i merge the PurchasedAmount into the first row using LinQ?
This is probably a more general solution than you need – it will work even if the other values are scattered across rows. The main condition is that the
Namecolumn should identify rows that belong together.This will return a new
IEnumerablewith the items grouped byNameand the non-null values selected (same effect as moving PurchasedAmount to the first row and deleting the second in your case).Note that the code is based on the assumption that
LastVisit,PurchaseAmountandTaggedare nullable types (DateTime?,int?andbool?). Thus the usage ofHasValue. If, however, they are strings in your case, you have to use!string.IsNullOrEmpty()instead (as forCodeandCustomerID).