C# Linq GroupBy and Count field -


i need group data record date, count occurrence of values in field tipoescola, field can have values 1 or 2, , need total amount of records of grouped date , each value of tipoescola. sorry, know it's little confusing i'm going show using data have....

these records of db:

nome    email               tipoescola          datacadastro teste1  email@email.com         2           2015-07-16 10:29:34.140 teste1  email@email.com         1           2015-07-16 10:29:34.140 teste2  email@email.com         2           2015-07-16 10:31:55.337 teste3  email@email.com         2           2015-07-16 12:38:22.403 teste4  email@teste.com         1           2015-07-17 13:33:26.973 teste5  teste@teste.com         2           2015-07-17 13:49:13.850 

this managed far:

[0] { data = {16/07/2015 10:29:34}, tipo = 2, qntd = 1 } [1] { data = {16/07/2015 10:29:34}, tipo = 1, qntd = 1 } [2] { data = {16/07/2015 10:31:55}, tipo = 2, qntd = 1 } [3] { data = {16/07/2015 12:38:22}, tipo = 2, qntd = 1 } [4] { data = {17/07/2015 13:33:26}, tipo = 1, qntd = 1 } [5] { data = {17/07/2015 13:49:13}, tipo = 2, qntd = 1 } 

but want achieve:

[0] { data = {16/07/2015}, tipo1 = 1, tipo2 = 3, qntd = 4 } [1] { data = {17/07/2015}, tipo1 = 1, tipo2 = 1, qntd = 2 } 

this code used group:

var leadsportipo = db.lead_sejaescola.where(x => entityfunctions.truncatetime(x.datacadastro) >= datainicio && entityfunctions.truncatetime(x.datacadastro) <= datafim)        .groupby(n => new { n.datacadastro, n.tipoescola })        .select(n => new { data = n.key.datacadastro, tipo = n.key.tipoescola, qntd = n.count() }); 

and code used format date:

var chartdata = leadsportipo.asenumerable()       .select(x => new {                   data = x.data.tostring("dd/mm/yy"),                   tipo = x.tipo,                   quantidade = x.qntd }); 

thanks!

can try following?

var leadsportipo =   db.lead_sejaescola.select(x => new { data = entityfunctions.truncatetime(x.datacadastro),                                        tipo = x.tipoescola })                     .where(x => x.data >= datainicio && x.data <= datafim)                     .groupby(x => x.data)                     .select(g => new { data = g.key,                                        tipo1 = g.where(x => x.tipo == 1).count(),                                        tipo2 = g.where(x => x.tipo == 2).count(),                                        qntd = g.count() })                     .tolist() 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -