1

我的表像这样的表:

id  no.of sales    datetime 
1    14            2013-09-10
2    45            2013-09-09
3    18            2013-09-08
4    14            2013-08-12
5    15            2013-08-14
6    18            2013-07-12

在第 9 个月的上表中,有 3 行。第 8 个月有 2 条记录。我需要第 9 个月和第 8 个月的所有销售额的总和,依此类推。谁能告诉我如何实现这一点。我的查询:

DateTime frommonth = DateTime.Now.AddMonths(-3);
DateTime tomonth = DateTime.Now;
var result= from pa in cxt.Products
            where (pa.datevalue >= frommonth && pa.datevalue < tomonth)
                && pa.productname == productname
            orderby pa.datevalue descending
            select new { noofsales=pa.No_of_sales ,datetime=pa.datevalue};

通过使用上述查询,我​​得到了个人第 9 个月的记录。我不想要个人的销售数量,我需要所有第 9 个月的销售额、第 8 个月的销售额等等。请告诉我...

4

2 回答 2

3

你可以使用SqlFunctions.DatePart静态方法,它会被翻译成DATEPARTsql函数:

var results = from r in ctx.Products
              let year = SqlFunctions.DatePart("yy", r.datevalue)
              let month = SqlFunctions.DatePart("mm", r.datevalue)
              where (r.datevalue >= frommonth && r.datevalue < tomonth)
              group r by new { year, month } into g
              select new {
                  Year = g.Key.year,
                  Month = g.Key.month,
                  NoSales = g.Sum(x => x.No_of_sales)
              };
于 2013-09-10T06:14:04.127 回答
1
var result = from r in cxt.Products
             group r by r.datevalue.Month
             into g
             select new {Month = g.Key, Sum = g.Sum(p=>p.No_of_sales)};
于 2013-09-10T05:52:57.640 回答