Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
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
please give me a sloution

thnx
Posted
Updated 25-Apr-12 5:15am
v4
Comments
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
gauravupadhyay 25-Apr-12 11:49am    
@ SAKryukov --
did you understand question ??

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.
 
Share this answer
 
v4
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:

(Note: The following code fragments were written in LINQPad -www.linqpad.net)

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.Add(new MonthlyAmount (1, 1000));
    monthlyAmountsList.Add(new MonthlyAmount (4, 500));
    monthlyAmountsList.Add(new MonthlyAmount (7, 1000));
    monthlyAmountsList.Add(new MonthlyAmount (10, 500));

    monthlyAmountsList.Dump();

    List<monthlyamount> monthlyUpdatesList = new List<monthlyamount>();

    if (monthlyUpdatesList == null)
    {
        return;
    }

    monthlyUpdatesList.Add(new MonthlyAmount (1, 100));
    monthlyUpdatesList.Add(new MonthlyAmount (4, 50));

    monthlyUpdatesList.Dump();

    foreach(MonthlyAmount monthlyUpdate in monthlyUpdatesList)
    {
        (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

monthlyUpdatesList List<MonthlyAmount> (2 items)

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...
 
Share this answer
 
v3
C#
list1.forech(item => {
for (int i=0; i<=list2.count;i++)
if(item.monthid == list2[i].monthid)
item.amont -= list2[i].monthid;
};)
 
Share this answer
 
v2
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

what are possible mistakes i am doing Please help .

thnx
 
Share this answer
 
v2
Comments
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
gauravupadhyay 27-Apr-12 1:30am    
hi sir
thanks for your valuable reply .

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'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900