我在数据库中有一个Employee Attendance表,其中包含以下列:
ID | Date | EmployeeId | Day1 | Day2 | Day3.... | Day31
1 | .... | ...........| 1 | 1 | 1 ..... | 5
第1天至第31天具有范围从1到5的整数值,每个具有特定含义。假设我们在Day1到Day28的列下有1,在Day29到Day31的列下有5,这里我们有28个1和3个5对抗ID 1。
待办事项
我想要计算一个特定的ID,有多少列有值1,有多少有值2,有多少有值3,有多少有值4,有多少有值5.(使用实体框架) )
示例代码:
public class LeaveBalance
{
public int ShortLeave { get; set; }
public int HalfLeave { get; set; }
public int FullLeaves { get; set; }
public int Absent { get; set; }
public Guid EmpCode { get; set; }
public Employee Employee { get; set; }
public int TotalLeaves { get; set; }
}
我很难计算:
public List<LeaveBalance> ReturnCount(DateTime date)
{
List<LeaveBalance> count = new List<LeaveBalance>();
using (var _db = new AppDbContext())
{
var employee = _db.EmployeeAttendanceSheets
.Where(r => r.Date.Month == date.Date.Month && r.Date.Year == date.Date.Year)
.ToList();
foreach (var emp in employee)
{
count.Add(new LeaveBalance { Absent = })
}
}
return count;
}
请提前帮助和谢谢
答案 0 :(得分:0)
var item = context.Table.Find(id);
var data = item.GetType().GetProperties().Where(x => x.Name.Contains("Day"))
.Select(x => (int)x.GetValue(item)).ToList();
var answer = Enumerable.Range(1, 5)
.Select(x => new { number = x, count = data.Count(y => y == x) }).ToList();