Reputation: 1574
I have a query that is pulling data from the database. Its grouping the items by their CheckDate. I need to turn the CheckDate which is a standard DateTime into just the month so that is grouped by month with no other dates. I can't figure out how to do that.
var query = from ech in this.dbContext.EmployeeCheckHistories
where ech.CompanyID == GlobalVariables.CompanyID &&
ech.Employees.ClientID == GlobalVariables.Client
group ech by ech.CheckDate into echG
orderby echG.Key descending
select echG;
Upvotes: 0
Views: 70
Reputation: 6587
Your query-
var query = from ech in this.dbContext.EmployeeCheckHistories
where ech.CompanyID == GlobalVariables.CompanyID &&
ech.Employees.ClientID == GlobalVariables.Client
group ech by ech.CheckDate into echG
orderby echG.Key descending
select echG;
Could be simplified as-
var query = this.dbContext.EmployeeCheckHistories.Where(c=>
c.CompanyID == GlobalVariable.CompanyID
&& c.Employees.ClientID == GlobalVariables.ClientID).GroupBy(d=> d.CheckDate.Month);
Upvotes: 0
Reputation: 8446
I need to turn the CheckDate which is a standard DateTime into just the month
If it truly is a DateTime
, then this should do the trick. Just group by the Month property on CheckDate
.
var query = from ech in this.dbContext.EmployeeCheckHistories
where ech.CompanyID == GlobalVariables.CompanyID &&
ech.Employees.ClientID == GlobalVariables.Client
group ech by ech.CheckDate.Month into echG
orderby echG.Key descending
select echG;
If it is actually a Nullable<DateTime>
, then you will need to check that the value exists, then group by Month if it does.
var query = from ech in this.dbContext.EmployeeCheckHistories
where ech.CompanyID == GlobalVariables.CompanyID &&
ech.Employees.ClientID == GlobalVariables.Client &&
ech.CheckDate.HasValue
group ech by ech.CheckDate.Value.Month into echG
orderby echG.Key descending
select echG;
Upvotes: 1