C#Linq嵌套类-条件不起作用

时间:2019-04-20 23:21:21

标签: c# linq join nested

我有3整。我找到类别并加入CategoryProduct,Product。 它是可行的,但“ where p.IsActive == true”查询不起作用。 我不想加入“ p.IsActive == false” 我能做什么?

var categoryQueryable = from c in context.Categories
                     where c.SeoUrl == seoUrl
                     join cp in context.CategoryProducts.ToList() on c.CategoryId equals cp.CategoryId
                     join p in context.Products.ToList() on cp.ProductId equals p.ProductId                                         
                     where p.IsActive == true
                     select c;


Category category = new Category();
category = categoryQueryable.FirstOrDefault<Category>();

public class Category
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CategoryId { get; set; }
    public string SeoUrl { get; set; }

    public virtual List<CategoryProduct> CategoryProducts { get; set; }
}

public class CategoryProduct
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CategoryProductId { get; set; }
    public int CategoryId { get; set; }
    public int ProductId { get; set; }

    public virtual Category Category { get; set; }
    public virtual Product Product { get; set; }
}
 public classProduct
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public bool IsActive { get; set; }

    public virtual List<CategoryProduct> CategoryProducts { get; set; }
 }

1 个答案:

答案 0 :(得分:0)

谢谢您的回答。 结果现在只能获得有效产品。

 var categoryQueryable = from c in context.Categories
                         where c.SeoUrl.Trim() == seoUrl.Trim()
                         join cp in context.CategoryProducts.ToList() on c.CategoryId equals cp.CategoryId
                         join p in context.Products.Where(p => p.IsActive == true).ToList() on cp.ProductId equals p.ProductId
                         select c;