C# 使用联接表进行内部联接,以检索linq中的所有CategoryProducts
我有一个名为CategoryProducts的联接表,它有两个字段(CategoryId,ProductId)。我正试图找出一种正确的方法来获取一个类别中的所有产品对象 以下是我所拥有的:C# 使用联接表进行内部联接,以检索linq中的所有CategoryProducts,c#,linq,entity-framework,entity-framework-6,C#,Linq,Entity Framework,Entity Framework 6,我有一个名为CategoryProducts的联接表,它有两个字段(CategoryId,ProductId)。我正试图找出一种正确的方法来获取一个类别中的所有产品对象 以下是我所拥有的: // get list of all of the products in a category public static IEnumerable<Product> GetList(int categoryId) { using (var context = new AppContext
// get list of all of the products in a category
public static IEnumerable<Product> GetList(int categoryId)
{
using (var context = new AppContext())
{
var pList= (from p in context.Products
join cp in context.CategoryProducts on categoryId equals cp.CategoryId
select p).ToList();
return pList;
}
}
//获取类别中所有产品的列表
公共静态IEnumerable GetList(int categoryId)
{
使用(var context=new AppContext())
{
var pList=(来自context.Products中的p
在context.categoryId上加入cp.CategoryProducts等于cp.categoryId
选择p.ToList();
返回层;
}
}
试试这个:
var pList= (from p in context.Products
join cp in context.CategoryProducts on p.Id equals cp.ProductId
where cp.CategoryId==categoryId
select p).ToList();
您需要使用一些在每个源中共享的值进行连接,这些值可以比较是否相等。在Linq中,实体通常基于表之间的逻辑关系。例如,在您的案例中,我认为您在产品
和类别产品
之间存在一对多关系。正确的连接方式是使用Product
的PK和CategoryProduct
上关系的FK
此外,如果您的CategoryProduct
实体具有以下结构:
public class CategoryProduct
{
public int ProductId{get;set;}
public int CategoryId{get;set;}
public virtual Category Category{get;set;}
public virtual Product Product{get;set;}
}
您可以这样做:
var query= context.CategoryProducts.Where(cp=>cp.CategoryId==categoryId).Select(cp=>cp.Product);
试试这个:
var pList= (from p in context.Products
join cp in context.CategoryProducts on p.Id equals cp.ProductId
where cp.CategoryId==categoryId
select p).ToList();
您需要使用一些在每个源中共享的值进行连接,这些值可以比较是否相等。在Linq中,实体通常基于表之间的逻辑关系。例如,在您的案例中,我认为您在产品
和类别产品
之间存在一对多关系。正确的连接方式是使用Product
的PK和CategoryProduct
上关系的FK
此外,如果您的CategoryProduct
实体具有以下结构:
public class CategoryProduct
{
public int ProductId{get;set;}
public int CategoryId{get;set;}
public virtual Category Category{get;set;}
public virtual Product Product{get;set;}
}
您可以这样做:
var query= context.CategoryProducts.Where(cp=>cp.CategoryId==categoryId).Select(cp=>cp.Product);
试试这个:
var pList= (from p in context.Products
join cp in context.CategoryProducts on p.Id equals cp.ProductId
where cp.CategoryId==categoryId
select p).ToList();
您需要使用一些在每个源中共享的值进行连接,这些值可以比较是否相等。在Linq中,实体通常基于表之间的逻辑关系。例如,在您的案例中,我认为您在产品
和类别产品
之间存在一对多关系。正确的连接方式是使用Product
的PK和CategoryProduct
上关系的FK
此外,如果您的CategoryProduct
实体具有以下结构:
public class CategoryProduct
{
public int ProductId{get;set;}
public int CategoryId{get;set;}
public virtual Category Category{get;set;}
public virtual Product Product{get;set;}
}
您可以这样做:
var query= context.CategoryProducts.Where(cp=>cp.CategoryId==categoryId).Select(cp=>cp.Product);
试试这个:
var pList= (from p in context.Products
join cp in context.CategoryProducts on p.Id equals cp.ProductId
where cp.CategoryId==categoryId
select p).ToList();
您需要使用一些在每个源中共享的值进行连接,这些值可以比较是否相等。在Linq中,实体通常基于表之间的逻辑关系。例如,在您的案例中,我认为您在产品
和类别产品
之间存在一对多关系。正确的连接方式是使用Product
的PK和CategoryProduct
上关系的FK
此外,如果您的CategoryProduct
实体具有以下结构:
public class CategoryProduct
{
public int ProductId{get;set;}
public int CategoryId{get;set;}
public virtual Category Category{get;set;}
public virtual Product Product{get;set;}
}
您可以这样做:
var query= context.CategoryProducts.Where(cp=>cp.CategoryId==categoryId).Select(cp=>cp.Product);
如果这些实体之间存在关系,则只需选择
类别
并包括产品
:
var result = context.CategoryProducts.
Where(c=>c.CategoryId == categoryId ).Include(c => c.Products).ToList();
如果这些实体之间存在关系,则只需选择
类别
并包括产品
:
var result = context.CategoryProducts.
Where(c=>c.CategoryId == categoryId ).Include(c => c.Products).ToList();
如果这些实体之间存在关系,则只需选择
类别
并包括产品
:
var result = context.CategoryProducts.
Where(c=>c.CategoryId == categoryId ).Include(c => c.Products).ToList();
如果这些实体之间存在关系,则只需选择
类别
并包括产品
:
var result = context.CategoryProducts.
Where(c=>c.CategoryId == categoryId ).Include(c => c.Products).ToList();
抱歉,我忘了提到我没有使用导航属性,因为我接管的数据库中没有设置关系。@JTunney,products和productcategories没有外键..coolIm正在处理一个混乱的数据库。未设置任何关系。抱歉,我忘了提到我没有使用导航属性,因为在我接管的数据库中没有设置关系。@JTunney,products and productcategories without foreign key..coolIm正在处理一个混乱的数据库。未设置任何关系。抱歉,我忘了提到我没有使用导航属性,因为在我接管的数据库中没有设置关系。@JTunney,products and productcategories without foreign key..coolIm正在处理一个混乱的数据库。未设置任何关系。抱歉,我忘了提到我没有使用导航属性,因为在我接管的数据库中没有设置关系。@JTunney,products and productcategories without foreign key..coolIm正在处理一个混乱的数据库。未设置任何关系。