【发布时间】:2016-01-29 14:50:08
【问题描述】:
我有这样的课程:
public class ProductInCategory
{
public Guid Guid { get; set; }
public long ProductID { get; set; }
public long ProductCategoryID { get; set; }
public virtual Product Product { get; set; }
public virtual ProductCategory ProductCategory { get; set; }
}
public class Product
{
public virtual ICollection<ProductInCategory> ProductsInCategories { get; set; }
// and other fields and navigation properties not important for this example
}
现在我想执行查询,该查询使用实体框架获取所有产品,并使用特定的 ProductCategoryID 进行预加载:
using (var db = new EntityDataModel())
{
var node = db.Tree.FirstOrDefault(x => x.Guid == editedNode);
List<long> descentantIds = db.Tree
.Where(x => x.AncestorID == node.AncestorID)
.Select(x => x.DescendantID).ToList();
List<Product> products = db.Products
.Include("Details")
.Include("Prices")
.Include("Prices.Currency")
.Include("Prices.Seller")
.Include("Translations")
.Include("Translations.Language")
.Include("ProductsInCategories")
.Where(x => ... )) // how to filter by ProductsInCategories.ProductCategoryID (which in my case is descentantIds) ?
.ToList();
}
我认为我应该在 Where 子句中输入类似于 .Where(x => descentantIds.Contains(x.ProductsInCategories.ProductCategoryID)) 的内容,但这不起作用。
Here 是类似的解决方案,但我不知道如何在我的情况下应用它。
感谢您的建议!
【问题讨论】:
-
你到底想要什么?该descentantIds 包含ProductsInCategories 中的任何ID?全部?反过来呢?
-
我想实现相当于 SQL
WHERE ProductsInCategories.ProductCategoryID IN (1, 2, 3 .. n),但我看不到如何通过导航属性 ProductsInCategories 中 ProductCategoryID 的 lambda 表达式值获得。在我的情况下,声明是IQueryable<Product> -
ProductionCategories 是产品的 ICollection,您想要哪个 ProductCategoryID?
标签: c# entity-framework linq navigation-properties