Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C linq查询筛选器子集合_C#_Linq_Entity Framework 5 - Fatal编程技术网

C# C linq查询筛选器子集合

C# C linq查询筛选器子集合,c#,linq,entity-framework-5,C#,Linq,Entity Framework 5,你好 我有一个像下面这样的模型课 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 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;}

    }
此列表显示所有员工及其所有付款。 但我需要过滤每个员工支付的费用,这些费用显示为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);
}
我现在正在使用entityframework 5

我的研究对我来说不起作用

希望有人能帮助我


提前感谢各位

这可能是个不错的选择

var listing = from d in db.Payments
              .include("Employees")
              .where d.IsPosted == 1
               select d.Employees;
未测试,请修复错误

从pyaments开始,已发布的筛选器=1
,然后选择相关员工

尝试以下操作:它将为您提供一个匿名类型列表,其中包含员工及其付款

using (dbcontext ctx = new dbcontext())
{
    ctx.Connection.Open();

    var result = (from e in ctx.Employees
                  join p in ctx.Payments on e.employeeId equals p.employeeId
                  where p.IsPosted == 1
                  select new
                  {
                      Employee = e,
                      Payments = p
                  }).ToList();

    ctx.Connection.Close();
}

您所要求的不是本机支持的,所以没有更简单的方法,但肯定有更有效的方法,因为您当前的代码正在执行N+1数据库查询

更好的方法是使用匿名类型投影通过一个数据库查询检索员工和相关的过滤付款,然后执行类似于您的方法的操作,在内存中创建最终结果。例如:

var listing = 
    db.Employees.Select(employee => new
    {
        employee,
        payments = employee.Payments.Where(p => p.IsPosted == 1)
    })
    .AsEnumerable() // Switch to LINQ to Objects
    .Select(r =>
    {
        r.employee.Payments = r.payments.ToList();
        return r.employee;
    })
    .ToList();

感谢Danilo Calzetta立即回复。。我会试试这个:DHi先生@Danilo Calzetta,还有别的办法吗?我需要绑定员工列表,然后绑定其付款的子报表。。这种方式将更加困难,因为我需要将每个付款循环到一个列表中。。因此,我可以将其绑定到我的WPF UI的列表视图。@Neil请看我的示例查询,它返回的正是di员工列表,因为选择d.employees而不是dhi@Paul我如何使用结果??我想把它封装到员工列表中,这样我就可以把它绑定到我的WPF用户界面上。很抱歉没有问题。它工作起来很有魅力!!!谢谢你帮助伊万爵士。。伊万爵士,我能问一下在林克哪里可以学到更多吗?有链接或书籍吗?希望收到您的来信,先生;。。。嗨,尼尔,我真的不知道该给你提什么建议。我相信LINQ有很多好书/链接,但我个人从MSDN主题和主要实验中学到了一切。请注意,不同的LINQ实现都有其特殊性,例如,上述内容可能适用于最新的EF核心,也可能不适用于最新的EF核心。是否有在ef6中使用LINQ的等效方法?我在ef6中尝试了相同的逻辑,但它没有过滤子集合。@重载它在ef6中应该可以工作。只需确保集合导航属性不是虚拟的,或者禁用了延迟加载,因为延迟加载将覆盖过滤。@IvanStoev,这非常有效!在第一个选择中,它过滤付款并为其创建var。在第二个选择中,它将从过滤列表中设置父列表。明亮的
using (dbcontext ctx = new dbcontext())
{
    ctx.Connection.Open();

    var result = (from e in ctx.Employees
                  join p in ctx.Payments on e.employeeId equals p.employeeId
                  where p.IsPosted == 1
                  select new
                  {
                      Employee = e,
                      Payments = p
                  }).ToList();

    ctx.Connection.Close();
}
var listing = 
    db.Employees.Select(employee => new
    {
        employee,
        payments = employee.Payments.Where(p => p.IsPosted == 1)
    })
    .AsEnumerable() // Switch to LINQ to Objects
    .Select(r =>
    {
        r.employee.Payments = r.payments.ToList();
        return r.employee;
    })
    .ToList();