C# 具有多对多的LINQ select语句
我有一个关系,项目类别。这个关系是多对多的,所以我有三个表:Project/Project\u has\u category/categories 我需要选择所有与某个类别相关的项目(按其id) 项目类 但这就产生了错误: 匹配的最佳重载方法 'System.Collections.Generic.ICollection.Contains(LibModels.Category)' 有一些是无效的 参数C:\Users\thomas\Desktop\Freelauncher 1005\Freelauncher\Controllers\ProjectController.csC# 具有多对多的LINQ select语句,c#,linq,C#,Linq,我有一个关系,项目类别。这个关系是多对多的,所以我有三个表:Project/Project\u has\u category/categories 我需要选择所有与某个类别相关的项目(按其id) 项目类 但这就产生了错误: 匹配的最佳重载方法 'System.Collections.Generic.ICollection.Contains(LibModels.Category)' 有一些是无效的 参数C:\Users\thomas\Desktop\Freelauncher 1005\Freela
我做错了什么 类别是
类别
对象的列表,您不能使用包含
方法搜索整数id(检查此方法的签名-需要搜索类别
对象):
用于检查是否存在id等于您的值的类别对象:
var projects = Adapter.ProjectRepository.Get()
.Where(x => x.Categories.Any(c => c.CategoryID == catID))
或者可能
var projects = Adapter.CategoryRepository
.Get()
.Where(c => c.CategoryID == catID)
.Select(c => c.Projects)
.Distinct();
要从另一个方向查询它,需要先将ID转换为int,然后使用Any
var id = int.Parse(catID);
Adapter.ProjectRepository.Get().Where(x => x.Categories.Any(y => y.CategoryID == id))
var projects = Adapter.ProjectRepository.Get()
.Where(x => x.Categories.Contains(catID)) // error
var projects = Adapter.ProjectRepository.Get()
.Where(x => x.Categories.Any(c => c.CategoryID == catID))
var projects = Adapter.ProjectRepository
.Get()
.Where(p => p.Categories
.Any(c => c.CategoryID == catID));
var projects = Adapter.CategoryRepository
.Get()
.Where(c => c.CategoryID == catID)
.Select(c => c.Projects)
.Distinct();
var id = int.Parse(catID);
Adapter.ProjectRepository.Get().Where(x => x.Categories.Any(y => y.CategoryID == id))