entity framework select last month records

后端 未结 2 1730
攒了一身酷
攒了一身酷 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<S_EmployeeSalary> 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<S_EmployeeSalary> moduleItems = db.S_EmployeeSalary
        .Include(x => x.C_UserItems)
        .Where(x => x.TypeId == typeId && 
                    x.SalaryDate >= firstDay);
    
    0 讨论(0)
  • 2021-01-06 14:44

    looks like this part is not going to return a result since it cant be bigger and smaller than 30 at the same time.

    DbFunctions.DiffDays(x.SalaryDate, DateTime.Now) > 30 && DbFunctions.DiffDays(x.SalaryDate, DateTime.Now) < 30)
    
    0 讨论(0)
提交回复
热议问题