The following code can be used to obtain the result set specified in the question
void Main()
{
List<fee> list1 = new List<fee> {
new Fee(1,1000),new Fee(4,500),
new Fee(7,1000),new Fee(10,500)
};
List<fee> list2 = new List<fee> {
new Fee(1,100),new Fee(4,50)
};
var resultSet = list1.Select (fee =>
new Fee(fee.MonthId, fee.Amount -
list2.FirstOrDefault (f => f.MonthId==fee.MonthId).Amount)
).ToList();
}
public struct Fee{
public int MonthId;
public decimal Amount;
public Fee(int monthId, decimal amount){
MonthId = monthId;
Amount = amount;
}
}
Note:
The
FirstOrDefault
extension method of
IEnumerable
interface returns the First found value satisfying the predicate or the default value of the item. In this case the
Fee
struct is Value type, hence we get
Amount = 0
for the default item, when the
MonthId
is not present in
list2
.
If the
Fee
is a class, then when the
MonthId
is not present in
list2
,
null
will be returned, as the default value for
reference type
is null. In such case we have check for
null
, before deducting the
amount
from
list2
from the amount of
list1
.