Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I am facing an prob please help.

I have following class.
C#
class Product
{
        public int ProdCode { get; set; }
        public decimal NetAmount { get; set; }
        public List<int> BillDetailIds { get; set; }
}


In db there are two tables:

Table 1: Master table : containing following columns
BillId (PK), BillDate
Table 2: Details Table: containing following columns
BillDetailId (PK), BillId (FK), ProdCode, NetAmount

I want to show list from details table group by with Prodcode with sum of its amount. so i did below


C#
var prodlist=Db.Details..GroupBy(x=>new{x.ProdCode}).select(x=>
                 new Product(){
                    ProdCode = Convert.ToInt32(x.FirstOrDefault().ProdCode),
                    NetAmount = x.Sum(s => s.Amount),
                    BillDetailIds=??? 
};


I'm stuck in BillDetailIds.
I need to store BillDetailIds on which groups is performed to create an Product object.


Help me please, thanks in advance
Posted
Updated 8-Aug-12 16:40pm
v2

1 solution

You could use this to resolve the problem:

C#
 var prodlist = Db.Details.GroupBy(x=>new {x.ProdCode}).Select(x=>new Product()
{
   ProdCode = Convert.ToInt32(x.FirstOrDefault().ProdCode),
   NetAmount = x.Sum(s => s.Amount),
   BillDetailIds = Db.Details.Where(
                 z=>z.ProdCode == x.FirstOrDefault().ProdCode)
                 .Select(z=>z.BillDetailId).ToList()
});


I hope this helps you

Kind regards
 
Share this answer
 
v2
Comments
tanishtaman 9-Aug-12 13:41pm    
thanks...it was so simple but didn't strike in my mind. thanks for replying.

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