1

I am building an ASP.NET MVC 3 application, where I have on list of custom objects according to this model:

public class AbnAmroTransaction
{
    public int TransactionId { get; set; }
    public int AccountNumber { get; set; }
    public string Currency { get; set; }
    public int TransactionDate { get; set; }
    public int InterestDate { get; set; }
    public decimal StartBalance { get; set; }
    public decimal EndBalance { get; set; }
    public decimal Amount { get; set; }
    public string Description { get; set; }
    public int CategoryId { get; set; }
}

I also have second list with slightly similar objects of custom object type "Transaction" (which I get from my SQL Server 2008 database using a DBML):

Transaction LinqToSql Object

int TransactionId
int ImportId
int CategoryId
DateTime DateTime
Nvarchar(MAX) Currency
Money Amount
Nvarchar(MAX) Description

I'm trying to create a 3rd list that contains all AbnAmroTransactions, where the AbnAmroTransaction.TransactionId is not in the Transactions list (TransactionId). How do I do this without having to loop through both lists, which seems like a very unefficient way to do it?

I have found this article: http://msdn.microsoft.com/en-us/library/system.collections.iequalitycomparer.equals.aspx But that only seems to apply to objects of the same type.

Erwin1441
  • 263
  • 1
  • 5
  • 19
  • I managed to fix it by typecasting all my AbnAmroTransaction objects to Transactions and then making a custom IEQualityComparer for Transaction! :) Thanks guys! – Erwin1441 May 23 '12 at 01:57

1 Answers1

3

If you are using linq then look at Except.

var list1 = new List<int>() {1,2,3,4,5};
var list2 = new List<int>() {4,5,6,7};
var newList = list1.Except(list2);

New list will contain {4,5}

But you would have to any some check for transaction Id first.

PMC
  • 4,698
  • 3
  • 37
  • 57
  • I think the types of the lists are different (custom object type vs original type). So I don't think that can work. – dcp May 22 '12 at 22:12
  • 2
    dcp, perhaps you should implement your classes with a IEqualityComparer? http://msdn.microsoft.com/en-us/library/bb336390.aspx – Codeman May 22 '12 at 22:16