【发布时间】:2023-03-14 14:44:01
【问题描述】:
我正在尝试使用 EF6 从数据库中获取数据。 我有两节课
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;}
}
我只需要获得已启用产品的已启用制造商 我尝试了以下方法:
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)
});
不幸的是,子对象没有被填充。 “产品”列表总是充满未“启用”的产品 有什么想法吗?
【问题讨论】:
-
您不能使用导航属性来访问相关实体的子集。
-
我害怕这样的事情。我将使用 Dapper 进行基本数据访问
标签: c# linq linq-to-entities entity-framework-6