我的员工薪资表包含:
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; }
}
当用户点击它时我有一个按钮,它会自动创建上个月记录的副本,只更新创建的新月记录:
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天
答案 0 :(得分:7)
取决于您是否需要上个月或过去30天(问题不清楚)
这是上个月:
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);
答案 1 :(得分:1)
看起来这部分不会返回结果,因为它不能同时变大和小于30。
DbFunctions.DiffDays(x.SalaryDate, DateTime.Now) > 30 && DbFunctions.DiffDays(x.SalaryDate, DateTime.Now) < 30)