如何在c#中将特定条件的项目添加到sql中添加特定条件的项后对sql进行分组 [英] How to group certain element then after add item of specific criteria doing linq to sql in c#

查看:69
本文介绍了如何在c#中将特定条件的项目添加到sql中添加特定条件的项后对sql进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个愚蠢的问题,但我确实需要帮助.我不常发问题,但是这次我真的帮了忙.

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

我需要一个对多个列进行分组的linq to sql查询.不仅如此,其中一列具有特定意义,也需要基于certain condition进行分组.

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.

我要查询的就是这个.

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;
        } 

我正在寻找的结果是将"Heure_Livraison"列按特定的首字母缩写进行分组.

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

查询结果如下.

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

现在,我想添加所有具有相同名称,相同Nb_Part的蛋糕Between 10-12.所以结果将是

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 !!!``

推荐答案

我终于可以通过创建一个单独的列并指定要存储在该列中的数据来解决该问题,以便在查询后仅需选择一个列.

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 querying I just have to pick that column.

感谢您发表评论!

这篇关于如何在c#中将特定条件的项目添加到sql中添加特定条件的项后对sql进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆