我做了这个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);
有没有办法做到这一点?
答案 0 :(得分:3)
如果您使用的是实体框架,则可以使用SqlFunctions.DatePart
.GroupBy(x => SqlFunctions.DatePart("m", x.date))
答案 1 :(得分: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;