如何使用 Entity Framework 计算具有特定值的列数
How to count number of columns having a certain values using Entity Framework
我在数据库中有一个员工出勤 table,包含这些列:
ID | Date | EmployeeId | Day1 | Day2 | Day3.... | Day31
1 | .... | ...........| 1 | 1 | 1 ..... | 5
Day1 到 Day31 有从 1 到 5 的整数值,每个值都有特定的含义。假设我们在第 1 天到第 28 天的列下有 1 个,在第 29 天到第 31 天的列下有 5 个,这里我们有 28 个 1 和 3 个 5 对应 ID 1.
待办事项
我想针对特定 ID 进行计数,即有多少列的值为 1,有多少列的值为 2,有多少列的值为 3,有多少列的值为 4,有多少列的值为 5。(使用Entity Framework)
示例代码:
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;
}
请帮助并提前致谢
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();
我在数据库中有一个员工出勤 table,包含这些列:
ID | Date | EmployeeId | Day1 | Day2 | Day3.... | Day31
1 | .... | ...........| 1 | 1 | 1 ..... | 5
Day1 到 Day31 有从 1 到 5 的整数值,每个值都有特定的含义。假设我们在第 1 天到第 28 天的列下有 1 个,在第 29 天到第 31 天的列下有 5 个,这里我们有 28 个 1 和 3 个 5 对应 ID 1.
待办事项
我想针对特定 ID 进行计数,即有多少列的值为 1,有多少列的值为 2,有多少列的值为 3,有多少列的值为 4,有多少列的值为 5。(使用Entity Framework)
示例代码:
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;
}
请帮助并提前致谢
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();