Skip to main content
deleted 48 characters in body
Source Link
Alberto Monteiro
  • 6.3k
  • 2
  • 31
  • 40

So i have a List called transactions.

 var itemA = new TransactionItem() { ProductId = 1, Quantity = 2 };  var itemB = new TransactionItem() { ProductId = 1, Quantity = 3 };  var tranA = new Transaction() { Type = TransactionType.credit, Items = new List<TransactionItem>() { itemA } };  var tranB = new Transaction() { Type = TransactionType.credit, Items = new List<TransactionItem>() { itemB } };  var tranC = new Transaction() { Type = TransactionType.debit, Items = new List<TransactionItem>() { itemA } };  var transactions = new List<Transaction>() { tranA, tranB }; 

How do I get a grouping that cancels out Credits and Debits; ie in the above I have two credits where ProductID equals 1, totaling 5, and one debit where ProductID equals 1, totaling 2, so I'd like to project a new list of transactionItems showing the resulting positive amount.

(background: I'm trying to create a function that takes a list of transactions and determines after all the credits and debits what items a person should have remaining on account.)

So i have a List called transactions.

 var itemA = new TransactionItem() { ProductId = 1, Quantity = 2 };  var itemB = new TransactionItem() { ProductId = 1, Quantity = 3 };  var tranA = new Transaction() { Type = TransactionType.credit, Items = new List<TransactionItem>() { itemA } };  var tranB = new Transaction() { Type = TransactionType.credit, Items = new List<TransactionItem>() { itemB } };  var tranC = new Transaction() { Type = TransactionType.debit, Items = new List<TransactionItem>() { itemA } };  var transactions = new List<Transaction>() { tranA, tranB }; 

How do I get a grouping that cancels out Credits and Debits; ie in the above I have two credits where ProductID equals 1, totaling 5, and one debit where ProductID equals 1, totaling 2, so I'd like to project a new list of transactionItems showing the resulting positive amount.

(background: I'm trying to create a function that takes a list of transactions and determines after all the credits and debits what items a person should have remaining on account.)

So i have a List called transactions.

var itemA = new TransactionItem() { ProductId = 1, Quantity = 2 }; var itemB = new TransactionItem() { ProductId = 1, Quantity = 3 }; var tranA = new Transaction() { Type = TransactionType.credit, Items = new List<TransactionItem>() { itemA } }; var tranB = new Transaction() { Type = TransactionType.credit, Items = new List<TransactionItem>() { itemB } }; var tranC = new Transaction() { Type = TransactionType.debit, Items = new List<TransactionItem>() { itemA } }; var transactions = new List<Transaction>() { tranA, tranB }; 

How do I get a grouping that cancels out Credits and Debits; ie in the above I have two credits where ProductID equals 1, totaling 5, and one debit where ProductID equals 1, totaling 2, so I'd like to project a new list of transactionItems showing the resulting positive amount.

(background: I'm trying to create a function that takes a list of transactions and determines after all the credits and debits what items a person should have remaining on account.)

Source Link
Evan Morrison
  • 702
  • 1
  • 6
  • 18

Using LINQ to group and sum

So i have a List called transactions.

 var itemA = new TransactionItem() { ProductId = 1, Quantity = 2 }; var itemB = new TransactionItem() { ProductId = 1, Quantity = 3 }; var tranA = new Transaction() { Type = TransactionType.credit, Items = new List<TransactionItem>() { itemA } }; var tranB = new Transaction() { Type = TransactionType.credit, Items = new List<TransactionItem>() { itemB } }; var tranC = new Transaction() { Type = TransactionType.debit, Items = new List<TransactionItem>() { itemA } }; var transactions = new List<Transaction>() { tranA, tranB }; 

How do I get a grouping that cancels out Credits and Debits; ie in the above I have two credits where ProductID equals 1, totaling 5, and one debit where ProductID equals 1, totaling 2, so I'd like to project a new list of transactionItems showing the resulting positive amount.

(background: I'm trying to create a function that takes a list of transactions and determines after all the credits and debits what items a person should have remaining on account.)