entity framework select last month records

后端 未结 2 1731
攒了一身酷
攒了一身酷 2021-01-06 14:20

I have employee salary table that contains :

public partial class S_EmployeeSalary
    {
        public int SalaryId { get; set; }
        public int TypeId         


        
2条回答
  •  不思量自难忘°
    2021-01-06 14:35

    Depending if you want the last month or last 30 days (question is unclear)

    This is for the previous month:

    var startOfTthisMonth = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);       
    var firstDay = startOfTthisMonth.AddMonths(-1);
    var lastDay = startOfTthisMonth.AddDays(-1);
    
    IQueryable moduleItems = db.S_EmployeeSalary
        .Include(x => x.C_UserItems)
        .Where(x => x.TypeId == typeId && 
                    x.SalaryDate >= firstDay &&
                    x.SalaryDate <= lastDay);
    

    This is for the previous 30 days:

    var firstDay = DateTime.Today.AddDays(-30);
    
    IQueryable moduleItems = db.S_EmployeeSalary
        .Include(x => x.C_UserItems)
        .Where(x => x.TypeId == typeId && 
                    x.SalaryDate >= firstDay);
    

提交回复
热议问题