Entity framework : Return grouped by value and sum of some columns in the same query

c# entity-framework entity-framework-6 iqueryable sql

Accepted Answer

Put Sum and Count to use.

 query = from record in context.tables
                group record by new
                {
                    record.Col3,
                    record.Col4,
                    record.Col5,
                } into g
                select new QueryResult
                {
                    Col1 = g.Sum(x => x.Col1),
                    Col2 = g.Sum(x => x.Col2),
                    Col3 = g.Key.Col3,
                    Col4 = g.Key.Col4,
                    Col5 = g.Key.Col5,
                    col6 = g.Count()

                };
5
6/7/2016 8:19:00 AM

Popular Answer

Try the following:

var query = context.tables.GroupBy(r=>new{r.Col3, r.Col4, r.Col5})
                          .Select(g=>{
                                         Col1 = g.Sum(c=>c.Col1),
                                         Col2 = g.Sum(c=>c.Col2),
                                         Col3 = g.Key.Col3,
                                         Col4 = g.Key.Col4,
                                         Col5 = g.Key.Col5,
                                         Col6 = g.Count
                                     })


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow