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
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);
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With