我正在尝试使用 Linq 查询我的数据库。简而言之,我的 linq 语句没有返回我想要的数据并且我遇到了错误。
public class Product{
[Key]
public int id{get;set;}
[Required]
[MinLength(3)]
public string Name{get;set;}
[MinLength(10)]
public string Description{get;set;}
[Required]
[Range(0, double.MaxValue)]
public decimal Price{get;set;}
public DateTime CreatedAt{get;set;} = DateTime.Now;
public DateTime UpdatedAt{get;set;} = DateTime.Now;
public List<ProductCategory> ProductCategories{get;set;}
}
public class Category{
[Key]
public int id{get;set;}
[Required]
[MinLength(2)]
public string Name{get;set;}
public DateTime CreatedAt{get;set;} = DateTime.Now;
public DateTime UpdatedAt{get;set;} = DateTime.Now;
public List<ProductCategory> ProductCategories{get;set;}
}
public class ProductCategory{
[Key]
public int id{get;set;}
public int ProductId{get;set;}
public int CategoryId{get;set;}
public Product Product{get;set;}
public Category Category{get;set;}
}
#Variable used in troublesome code (in controller)
Product product = context.Products
.Include(p => p.ProductCategories)
.ThenInclude(pc => pc.Category)
.FirstOrDefault(p => p.id == id);
#Troublesome code (in controller)
List<Category> categories = context.Categories
.Include(c => c.ProductCategories)
.ThenInclude(pc => pc.Product)
.Where(c => c.ProductCategories.Select(pc => pc.Product) != product)
.ToList();
产品和类别具有多对多的关系。我希望类别变量包含不在检索到的产品中的所有类别的列表。有人不能引导我朝着正确的方向前进或告诉我我在这里做错了什么吗?
错误:'System.Nullable 1[System.Int32]' cannot be used as the data type for a sequence with an ItemExpression of type 'System.Nullable1 [System.Int32]'