Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

entity framework select last month records

I have employee salary table that contains :

public partial class S_EmployeeSalary
    {
        public int SalaryId { get; set; }
        public int TypeId { get; set; }
        public int UserId { get; set; }
        public double Salary { get; set; }
        public Nullable<double> ExtraSalary { get; set; }
        public Nullable<double> Insurance { get; set; }
        public Nullable<double> Sanctions { get; set; }
        public System.DateTime SalaryDate { get; set; }

        public virtual C_UserItems C_UserItems { get; set; }
    }

and i have a button when user click on it, it created a copy from last month records automatically to just update records of new month that created:

public ActionResult CreateNewRows(int typeId)
{
    IQueryable<S_EmployeeSalary> moduleItems = db.S_EmployeeSalary
        .Include(x => x.C_UserItems)
        .Where(x => x.TypeId == typeId && DbFunctions.DiffDays(x.SalaryDate, DateTime.Now) > 30 && DbFunctions.DiffDays(x.SalaryDate, DateTime.Now) < 30);

    foreach (var item in moduleItems)
    {
        S_EmployeeSalary entity = new S_EmployeeSalary
        {
            TypeId = typeId,
            UserId = item.UserId,
            Salary = item.Salary,
            ExtraSalary = item.ExtraSalary,
            Insurance = item.Insurance,
            Sanctions = item.Sanctions,
            SalaryDate = DateTime.Now
        };
        db.S_EmployeeSalary.Add(entity);
    }

    db.SaveChanges();
}

my question is how can i just specify last month records only to select it, i tried DbFunctions.DiffDays but it didnt work well, is there anyone can help me for new idea ? note its employees salary , so i need last month not last 30 days

like image 827
Ahmad Alaa Avatar asked Jun 11 '15 10:06

Ahmad Alaa


2 Answers

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);
like image 178
DavidG Avatar answered Sep 28 '22 19:09

DavidG


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)
like image 25
Gökhan Uslu Avatar answered Sep 28 '22 20:09

Gökhan Uslu