c# - Entity Framework 6 过滤子对象

标签 c# linq linq-to-entities entity-framework-6

我正在尝试使用 EF6 从数据库中获取数据。 我有两个类(class)

public class Manufacturer
{
  public int Id {get;set;}
  public string Name {get;set;}
  public bool Enabled {get;set;}
  public virtual IList<Product> Products {get;set;}
}

public class Product
{
  public int Id {get;set;}
  public string Name {get;set;}
  public bool Enabled {get;set;}
  public virtual Manufacturer Manufacturer {get;set;}
}

我只需要获得 Enabled Manufacturer with Enabled Products 我尝试了以下方法:

var results = _context.Manufacturer
              .Where(m => m.Enabled)
              .Where(m => m.Products.Any(p => p.Enabled))
              .Select(m => new
              {
                  Manufacturer = m;
                  Product = m.Products.Where(p => p.Enabled)
              });

不幸的是,子对象没有被填充。 “产品”列表总是充满未“启用”的产品 有什么想法吗?

最佳答案

你可以反过来。也尝试包括相关对象。

var r = context.Products.Include("Manufacturer")
               .Where(p=>p.Enabled && p.Manufacturer.Enabled);

关于c# - Entity Framework 6 过滤子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27912597/

相关文章:

c# - 打开 Visual Studio 2012 时编译时间变慢

c# - Windows Phone - 评分和评论命令

c# - 当 ID 自动递增时,如何在 Visual Studio 中将其他值插入数据库

c# - .net + SQL + 超时问题

c# - LINQ-to-SQL 编译查询问题(用作未编译查询)

c# - 按查询中的行字段降序排序

.net - Entity Framework linq - 我应该学习哪个,基于查询的方法?

c# - 从同一个项目编译 32 位和 64 位?

c# - LINQ Max() 函数执行速度超慢

c# - 对象必须实现 IConvertible