Grouping query by date

Asked

Viewed 243 times

1

I am developing a query with C#, but I have a problem to accomplish it. I need to group by date.

Code:

public void BuscarVendaDeProdurtos_Saidas()
{
    var data1 = Convert.ToDateTime(dateINI_Saida.Text);
    var data2 = Convert.ToDateTime(dateFINI_Saida.Text);
    var cod = Convert.ToInt64(textBox8.Text);

    var pesquisa = from p in dc.ITEMVENDA
    orderby p.CD_PRODUTO descending
    where p.CD_PRODUTO == cod && p.Data_Venda >= data1 && p.Data_Venda <= data2
    select new
    {

        p.NM_PROD,
        p.Data_Venda,
        p.QT_PRODV,
    };

    dataGridView2.DataSource = pesquisa.ToList();

    Soma = 0;
    foreach (DataGridViewRow dr in dataGridView2.Rows)
    Soma += Convert.ToDecimal(dr.Cells[2].Value);
    textBox12.Text = Convert.ToString(string.Format("{0:n}", Soma));
}

This research returns this:

Data Produto      Qt
20/10/2017……………….  3
20/10/2017 …………….. 1

I need something like that:

Data Produto     Qt
20/10/2017………………. 4

A single date already with its amounts summed.

  • I don’t understand c#, so I’m sorry if I talk nonsense, but is this data coming from a database? Why not use direct SQL, like select data, count(*) from tabela group by data order by data?

2 answers

0

Try group by with a date formatted with dd/mm/yyyy

public void BuscarVendaDeProdurtos_Saidas()
{
    var data1 = Convert.ToDateTime(dateINI_Saida.Text);
    var data2 = Convert.ToDateTime(dateFINI_Saida.Text);
    var cod = Convert.ToInt64(textBox8.Text);

    var pesquisa = from p in dc.ITEMVENDA
    orderby p.CD_PRODUTO descending
    where p.CD_PRODUTO == cod && p.Data_Venda >= data1 && p.Data_Venda <= data2
    select new
    {

        p.NM_PROD,
        p.Data_Venda,
        p.QT_PRODV,
    };

    dataGridView2.DataSource = pesquisa // .ToList() 
    //.OrderBy(x => DateTime.ParseExact(x.Data_Venda, "dd/MM/yyyy", null))
        .OrderBy(x => x.Data_Venda.ToString("dd/mm/yyyy"))
        .ToList();
    ;

    Soma = 0;
    foreach (DataGridViewRow dr in dataGridView2.Rows)
    Soma += Convert.ToDecimal(dr.Cells[2].Value);
    textBox12.Text = Convert.ToString(string.Format("{0:n}", Soma));
}
  • hello friend the method you explained to me returns the following error:LINQ to Entities does not recognize the 'System.Datetime Parseexact (System.String, System.String, System.Iformatprovider) method, and this method cannot be translated to a store expression. '

  • use Tolist() ... I left commented

0

You can use the Groupby from the LIN to group.

I don’t know what the attributes name of your object is but try to change this line:

dataGridView2.DataSource = pesquisa.ToList();

For something like this:

dataGridView2.DataSource = pesquisa.GroupBy(x => x.Data_Venda, x => x.Quantidade).ToList();

I believe this function will group the sales dates adding up the quantities

  • hello friend as you suggested when I do the query in GRID. just shows me the date, ie it’s not working yet

  • You’re right, I’m sorry, with what I said he puts together a list grouping datetime as Key and a list of integers. You will need an auxiliary list to store the values, it looks like this: var auxLista = pesquisa.GroupBy(x => x.Data, x => x.Quantidade).ToList();&#xA; var listaProdutos = new List<Produto>();&#xA; foreach (var aux in auxLista)&#xA; {&#xA; listaProdutos.Add(new Produto {Data = aux.Key, Quantidade = aux.Sum()});&#xA; }

  • Amigo Muito Thanks for your help in solving the problem of my query plus the procedure you Suggest to Sima did not work ta giving error in this function Sum

  • Putz, but is your attribute numerical type? If it’s not a problem, send the structure of your class so I can take a look

  • my Email and [email protected] Check out a hi la that I send the code to Check out and some prints. from the screen

Browser other questions tagged

You are not signed in. Login or sign up in order to post.