Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有多对多的LINQ select语句_C#_Linq - Fatal编程技术网

C# 具有多对多的LINQ select语句

C# 具有多对多的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

我有一个关系,项目类别。这个关系是多对多的,所以我有三个表:Project/Project\u has\u category/categories

我需要选择所有与某个类别相关的项目(按其id)

项目类 但这就产生了错误:

匹配的最佳重载方法 'System.Collections.Generic.ICollection.Contains(LibModels.Category)' 有一些是无效的 参数C:\Users\thomas\Desktop\Freelauncher 1005\Freelauncher\Controllers\ProjectController.cs


我做错了什么

类别是
类别
对象的列表,您不能使用
包含
方法搜索整数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))