My main problem description goes like this:
I have two lists of BankAccount
objects. A BankAccount
has properties such as BankCode
and AccountNumber
which uniquely identifies an account. So both the lists may contain the same bank account but they may have their Source
, Amount
, or AccountTypes
differing.
The aim here is to merge those two lists:
- add accounts to the first list if it is available in the second (but not in the first list).
- If the bank accounts are the same in both lists, update the details of the bank account in the first list with the details of the (matching) bank account in the 2nd list.
I've tried implementing the solution mentioned in one SO post. I've went and tried writing my code down at a .NET code pad site. But I am not able to get the output after trying to execute line no. 93 which I've commented.
class BankAccount
{
public string BankCode{get;set;}
public string AccountNumber{get;set;}
public string AccountType{get;set;}
public string Amount{get;set;}
public string Source{get;set;}
public override bool Equals(object obj)
{
var acc = obj as BankAccount;
return Equals(acc);
}
public override int GetHashCode()
{
return this.GetHashCode();
}
public bool Equals(BankAccount acc2)
{
if(acc2 == null) return false;
if(string.IsNullOrEmpty(acc2.BankCode)) return false;
if(string.IsNullOrEmpty(acc2.AccountNumber)) return false;
return this.BankCode.Equals(acc2.BankCode) && this.AccountNumber.Equals(acc2.AccountNumber);
}
}
//List<BankAccount> lst3 = lst.Union(lst1).ToList(); // line 93
Full code can be viewed here.
PS: I'm not sure if this could be a problem with the codepad site or not.
Update - Monday, 14 February 2011 - 4:50:24 (am) / 04:50:24 GMT
Thanx for the update. But something is still amiss. In the output, list 3's first item should have AccountType=P
and Source=lst2
. The 2nd requirement isn't met. I figure Union()
does only a part of what I need. What do I need to do satisfy the 2nd requirement.
EDIT by drachenstern: I'm not sure this title is any better, but it's definitely more informative than the previous title as to the actual question :\