Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
my list having no of items which is having id, itemid, itemname, qty, price etc.
list having one item more than ones then that qty will be added and price will be added. finally it having only one row for one item. how to write logic for this. any one can help me..

ex:

list having

id   ItemId  ItemName  Qty   Price
1      4      Idly      1    25.00
2      5      Dosa      2    50.00
3      4      Idly      2    50.00
4      2      vada      2    30.00
5      4      Idly      1    25.00


This list vill becom

id   ItemId  ItemName  Qty   Price
1      4      Idly      4    100.00
2      5      Dosa      2    50.00
4      2      vada      2    30.00



[edit]Code blocks added - OriginalGriff[/edit]
Posted
Updated 22-Jun-11 0:33am
v3
Comments
Sergey Alexandrovich Kryukov 22-Jun-11 14:39pm    
List view? WPF, Forms, ASP.NET, what?!
What is the problem? Working with list view is simple enough...
--SA

check this Link[^]
 
Share this answer
 
Comments
ajitha.pusapati 22-Jun-11 8:37am    
thanks this is really helpful.
Loop through those values(Array or something), check the ItemId is duplicate, if yes then SUM the qty values & Price values.
for(Condition)
{
 if(ItemId == TempItemId)//Check the ItemId for duplicate
 {
  //Sum those values
  Qty = Qty + TempQty;
  Price = Price + TempPrice;
  //Save into other object(Array or something), so you will get 3 rows instead of 5 rows finally
 }
}
 
Share this answer
 
v2
Try that way also

var newI = items.GroupBy(i => i.ItemId).Distinct();

foreach (var item in newI)
{
       Response.Write(item.First() + "<BR/>");
}
 
Share this answer
 

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