Entity Framework Linq IQueryable不包含包含的定义

问题描述 投票:0回答:1

我有以下(有效的)查询

return _context.SubCategories
           .Join(_context.CategoryLinks,
              sc => sc.ID,
              cl => cl.SubCategoryID,
              (sc, cl) => new { SubCategory = sc, CategoryLinks = cl })
           .Where(x => x.CategoryLinks.CategoryID != CategoryID)
           .Select(x => x.SubCategory).Distinct();

但是我的问题是,由于一个子类别可以链接到多个类别,因此最终将返回双记录。

因此,我们正在检索链接到类别Cat_1的子类别。Sub_A链接到Cat_1,但也链接到Cat_2,一旦链接到CategoryLink表中的Cat_1,查询就会从结果中过滤出Sub_A。但是最后,Sub_A仍然是结果集的一部分,因为它也链接到Cat_2

所以我想,如果我先检索子类别(请参见下面的代码),然后过滤掉包含已包含的子类别,它将解决我的问题。

var test = _context.CategoryLinks.Where(x => x.CategoryID == CategoryID);

            IQueryable<DM.SubCategory> aaa = _context.SubCategories
               .Join(_context.CategoryLinks,
                  sc => sc.ID,
                  cl => cl.SubCategoryID,
                  (sc, cl) => new { SubCategory = sc, CategoryLinks = cl })
               .Where(x => x.CategoryLinks.CategoryID != CategoryID && !test.Contains(x.SubCategory.ID))
               .Select(x => x.SubCategory);

但是尝试进行测试时出现错误。包含哪个;

'IQueryable<CategoryLink>' does not contain a definition for 'Contains' and the best extension method overload 'ParallelEnumerable.Contains<Guid>(ParallelQuery<Guid>, Guid)' requires a receiver of type 'ParallelQuery<Guid>

为什么不允许我在上述查询中使用包含?我尝试用“ IQueryable 测试”替换var测试,但这没关系。

c# entity-framework linq join contains
1个回答
0
投票
IQueryable<T>.Contains有两个重载方法,并且都不接受任意类型。如果要检查项目是否在集合中,请改用IQueryable<T>.Any

从您的代码段中,我想可能是test.Any(t => t.ID == t.Subcategory.ID)

© www.soinside.com 2019 - 2024. All rights reserved.