你好,
我有一个如下的模型类
public class EmployeeModel
{
[Key]
public int employeeId{get;set;}
public string Fullname {get;set;}
public string Address{get;set;}
public ICollection<PaymentModel> Payments {get;set;}
}
public class PaymentModel
{
[Key]
public int PaymentId{get; set;}
public int employeeId{get; set;}
public decimal PaymentAmount{get; set;}
public int IsPosted {get; set;}
public virtual EmployeeModel Employee {get; set;}
}
我想使用 LINQ 查询员工名单和他们的付款清单。我的代码如下:
dbcontext db = new dbcontext();
var listing = from d in db.Employees
.include("Payments")
select d;
这个列表展示了所有员工及其所有的薪资支付。但我需要筛选每个员工支付的IsPosted = 1。
所以作为初始答案,我会编写以下代码;
dbcontext db = new dbcontext();
List<EmployeeModel> FinalList = new List<EmployeeModel>();
var listingofEmp = db.employee.ToList();
foreach(EmployeeModel emp in listingofEmp){
emp.Payments= db.payments.where(x => x.IsPosted == 1).ToList();
FinalList.Add(emp);
}
我的问题是,是否有其他更简单的编码方式?例如这样。
dbcontext db = new dbcontext();
var listing = from d in db.Employees
.include(d => x.Payments.IsPosted == 1)
select d;
我目前正在使用Entity Framework 5。
我已经进行了相关研究,发现它对我不起作用链接。
希望有人能帮助我。
感谢大家的帮助。