1

我做了这个 LINQ 查询

var db = new DataEntities();
var records = db.Table_1.GroupBy(x => x.date).tolist();

其中 Table_1 的列属于类型

ColumnName       dataType

id               numeric
date             datetime

table_1 中的记录:

1          3/May/2013
2          4/Apr/2013
3          7/May/2013

我想根据月份对 Table_1 的所有记录进行分组

var records = db.Table_1.GroupBy(x => x.date.Month);

有什么办法吗?

4

2 回答 2

3

如果您使用的是实体框架,您可以使用SqlFunctions.DatePart

.GroupBy(x => SqlFunctions.DatePart("m", x.date))
于 2013-09-01T08:32:14.657 回答
1

我找到了正确的答案,这也可以帮助你......

  System.Globalization.DateTimeFormatInfo DFI=new System.Globalization.DateTimeFormatInfo();
            var InvoiceDetailMonthwise = (from t in clsGlobalObjectRefrances.SchoolSoulController.Stt.ssspAccInvoiceBook(clsSchoolSoulObjects.OAcdSchoolInfo.SchoolId,clsSchoolSoulObjects.OAcdSchoolInfo.CurrentActiveSessionId )
                                          group t by new { t.VDate.Value.Month} 
                                              into grp
                                              select new lcclsInvoiceBook
                                              {
                                                  Month=DFI.GetMonthName( grp.Key.Month).ToString(),
                                                  Debit = grp.Sum(t => t.DebitAmount) > 0 ? 0 : -(grp.Sum(t => t.DebitAmount)),
                                                  Credit = grp.Sum(t => t.CreditAmount ) > 0 ? (grp.Sum(t => t.CreditAmount )) : 0,

                                              }).ToList();



            dgvInvoiceBook.DataSource = InvoiceDetailMonthwise;
于 2013-09-01T09:24:59.660 回答