16,000,371 members
See more:
Sorry
now
Updated ----
hi all please give me the solution for this problem

i have two generic list having Data from two classes using GetAll Functions

i extracted two fields by using LINQ from both Class type Generic List.
LINQ Resultset have two fields in both Lists from them i need to data .

same columns .
monthId ,
Amount

first list have main records for a student month wise fee
while second have concessional record for that student month wise Concessional fee

like

List1 contains

MonthId | Amount
1 | 1000
4 | 500
7 | 1000
10 | 500

And List 2 Contains records like

MonthId | Amount
1 | 100
4 | 50

So i need to get Subtracted results like

MonthId | Amount
1 | 900
4 | 450
7 | 1000
10 | 500

Please give solution how i perform this ..

What should i do to get Result Set .

LINQ can help i tried but did not get right result in another var list

thnx
Posted
Updated 25-Apr-12 5:15am
v4
Sergey Alexandrovich Kryukov 25-Apr-12 10:43am
What is the exact type of your "generic list"? I never heard that a list could have columns... :-)
--SA
@ SAKryukov --
did you understand question ??

## Solution 1

The following code can be used to obtain the result set specified in the question

C#
```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;
}
}
//resultSet is List<fee>
//MonthId Amount
// 1 	  900
// 4 	  450
// 7	 1000
//10	  500```

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`.

v4

## Solution 2

Greetings;

In addition to VJ Reddy's solution, I think an extension method along the following lines might be an option for you as well. The extension method is called using a LINQ query for each of the object references that has an update pending inside a foreach() loop:

C#
```class MonthlyAmount
{
public int MonthID {get; set;}
public decimal Amount {get; set;}

public MonthlyAmount(int Month, decimal Amount)
{
this.MonthID = Month;
this.Amount = Amount;
}
};

static class Extension
{
public static void Update<t>(this T Item, Action<t> UpdateAction)
{
UpdateAction(Item);
}
};

void Main()
{
List<monthlyamount> monthlyAmountsList = new List<monthlyamount>();

if (monthlyAmountsList == null)
{
return;
}

monthlyAmountsList.Dump();

{
return;
}

{
(from entry in monthlyAmountsList
where entry.MonthID == monthlyUpdate.MonthID
select entry).First().Update(updatedEntry => updatedEntry.Amount -= monthlyUpdate.Amount);
}

monthlyAmountsList.Dump();

return;
}
```

And the Lists<> before the Update Extension is executed contain:

monthlyAmountsList List<MonthlyAmount> (4 items)

MonthID Amount
1 1000
4 500
7 1000
10 500

MonthID Amount
1 100
4 50

The contents of the Updated List<> after the foreach() loop would be as follows:

monthlyAmountsList List<MonthlyAmount> (4 items)

MonthID Amount
1 900
4 450
7 1000
10 500

I hope this was of help and interest to you.

Best Regards...

v3

## Solution 4

C#
```list1.forech(item => {
for (int i=0; i<=list2.count;i++)
if(item.monthid == list2[i].monthid)
item.amont -= list2[i].monthid;
};)```

v2

## Solution 3

dear VJ Sir

You are right i am getting null reference problem .

my code is :

for first List

C#
```var lisFeeStrdetail = listditinct.GroupBy(x => new { x.MonthId }).Select(group => new { monthId = group.Key.MonthId, Feesum = group.Sum(x => x.Amt) });

and for Second List

var concessionAmount = from c in lFeeConcessionDetails2
orderby c.MonthId ascending
select new { MonthId = c.MonthId, Feesum = c.Amount };

and for result Set

if (concessionAmount.Count() != 0)

{

var resultSet = lisFeeStrdetail.Select(a => new { monthId = a.monthId, Feesum = a.Feesum -  concessionAmount.FirstOrDefault(f => f.MonthId == a.monthId).Feesum  } ).ToList();

}

getting object reference not set to an instance of an Object exception

thnx```

v2
VJ Reddy 26-Apr-12 23:27pm
The reason is, in the statement concessionAmount.FirstOrDefault(f => f.MonthId == a.monthId).Feesum

FirstOrDefault returns default value when the required item is not found. In this case the default is null as the default value for reference type is null. So replace

concessionAmount.FirstOrDefault(f => f.MonthId == a.monthId).Feesum with

concessionAmount.FirstOrDefault(f => f.MonthId == a.monthId).Feesum == null ? 0 : concessionAmount.FirstOrDefault(f => f.MonthId == a.monthId).Feesum
hi sir

getting exception due to ---

the result of this expression is alway 'false' since a value of type 'decimal' is never equal to 'null' type of 'decimal'