实体框架选择上个月记录 [英] 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();
}
我的问题是我怎么能只指定上个月只记录来选择它,我想DbFunctions.DiffDays但它没有很好地工作,有任何人能帮助我对新的想法?
注员工的工资,所以我需要上个月没有最近30天
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
推荐答案
根据,如果你想在最后一个月或者最近30天(问题不清楚)
Depending if you want the last month or last 30 days (question is unclear)
这是前一个月:
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);
这是过去30天:
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);
这篇关于实体框架选择上个月记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!