How to group certain element then after add item of specific criteria doing linq to sql in c#

风流意气都作罢 提交于 2020-02-16 10:39:07

问题


This might seem like a stupid question but i really need help. I don't often post question but this time i really help.

I need to have a linq to sql query that groups multiple columns. But not just that, one of the columns have specific that also need to be grouped base on certain condition.

The Query i have is this one.

using (var donnée = new ClassDonnéeDataContext(mycontrng))
        {
            var don = from d in donnée.Reservations
                      where (d.Date_Livraison.Value.Date == startDate.Value.Date) && d.Sortie_Cuisine != "Oui" && d.Livraison != "Annulée" && (d.Reserv_Boutique == "Non" || d.Reserv_Boutique == null)
                      group d by new
                      {
                          Gateau = d.Gateau,
                          Heure = d.Heure_Livraison,
                          Nb_Part = d.Part,
                      } into grs
                      select new
                      {
                          Gateau = grs.Key.Gateau,
                          Heure = grs.Key.Heure,
                          Nombre = grs.Sum(x => x.Nombre),
                          Nb_Part = grs.Key.Nb_Part,
                      };

            var order = from ord in don
                        orderby ord.Heure ascending
                        select ord;

            dgv.DataSource = order;
        } 

The result i'm looking for is to have The columns "Heure_Livraison" to be grouped by specific critiria.

The result of the Query is as follow.

Gateau:                               Heure:                 Nombre:                  Nb_Part:

Foret Noire                           10                     2                        6
Ganache                               10                     2                        6
Foret Noire                           11                     2                        6
Ganache                               11                     2                        6
Ganache                               12                     1                        6

Now i want to add all the Cake of the same name, same Nb_Part Between 10-12. So the result Will like

Gateau:                               Heure:                 Nombre:                  Nb_Part:

Foret Noire                           10                     4                        6
Ganache                               10                     5                        6

Please if anyone has a suggestion to this question, give it to me !!!``


回答1:


I finally get to solve the problem by creating a separate column and specify the data to be stored in that column so that after when quering i just have to pick that column.

Thank you for comment !!!!



来源:https://stackoverflow.com/questions/59839662/how-to-group-certain-element-then-after-add-item-of-specific-criteria-doing-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!