【发布时间】:2013-11-25 12:22:32
【问题描述】:
我有两个类形成多对多关系。
public class Product
{
public int ID { get; set; }
public virtual ICollection<Category> Categories{ get; set; }
}
public class Category
{
public int ID { get; set; }
public virtual ICollection<Product> Products{ get; set; }
}
我需要选择包含在某个类别中的所有产品。例如 ID 为 1 的类别中的所有产品,或 ID 为 1 和 3 的类别中的所有产品。 如何在 Linq 中编写此查询?
注意: 我想用这个作为过滤技术。我有这个代码:
public IQueryable<Product> GVProducts_GetData(string Sort)
{
var query = _db.Products.AsQueryable();
//Here I want filter my data
if (Sort == "1")
{
query = (here should select all the product which are in category 1)
}
else if (Sort == "2")
{
query = (here should select all the product which are in category 2)
}
return query;
}
【问题讨论】:
标签: c# linq entity-framework ef-code-first