C# 如何检索使用linq中提供的所有标记进行标记的项目?

C# 如何检索使用linq中提供的所有标记进行标记的项目?,c#,linq-to-sql,tagging,C#,Linq To Sql,Tagging,我似乎在这方面遇到了麻烦。我有一个ID为的任务表和一个标记表,其中有一个标记字段和一个对任务表的外键约束 我希望能够通过标签执行和搜索任务。例如,如果我搜索带有标记“可移植性”和“测试”的任务,我不想要带有标记“可移植性”而不是“测试”的任务 我尝试了以下语法: var tasks = (from t in _context.KnowledgeBaseTasks where t.KnowledgeBaseTaskTags.Any(x => tag

我似乎在这方面遇到了麻烦。我有一个ID为的任务表和一个标记表,其中有一个标记字段和一个对任务表的外键约束

我希望能够通过标签执行和搜索任务。例如,如果我搜索带有标记“可移植性”和“测试”的任务,我不想要带有标记“可移植性”而不是“测试”的任务

我尝试了以下语法:

 var tasks = (from t in _context.KnowledgeBaseTasks
                     where t.KnowledgeBaseTaskTags.Any(x => tags.Contains(x.tag))
                     select KnowledgeBaseTaskViewModel.ConvertFromEntity(t)
                    ).ToList();
这当然是进行OR搜索,而不是AND搜索。我不知道如何把它变成一个搜索引擎

编辑
我还需要能够搜索任务包含的X个标记中的2个。因此,如果任务被标记为“bugfix”、“portability”、“testing”,并且我搜索“testing”和“portability”,那么该任务仍然会显示。

使用All而不是Any;并且为了只选择KnowledgeBaseTasks,它有所有的
标签(但可能更多);反转表达式:

var tasks = (from t in _context.KnowledgeBaseTasks
                 where tags.All(tag => t.KnowledgeBaseTaskTags.Contains(tag))
                 select KnowledgeBaseTaskViewModel.ConvertFromEntity(t)
                ).ToList();

使用全部而不是任何;并且为了只选择KnowledgeBaseTasks,它有所有的
标签(但可能更多);反转表达式:

var tasks = (from t in _context.KnowledgeBaseTasks
                 where tags.All(tag => t.KnowledgeBaseTaskTags.Contains(tag))
                 select KnowledgeBaseTaskViewModel.ConvertFromEntity(t)
                ).ToList();
你想做什么

LinqToSql可能如下所示:

List<int> myTags = GetTagIds();
int tagCount = myTags.Count;

IQueryable<int> subquery =
  from tag in myDC.Tags
  where myTags.Contains(tag.TagId)
  group tag.TagId by tag.ContentId into g
  where g.Distinct().Count() == tagCount
  select g.Key;

IQueryable<Content> query = myDC.Contents
  .Where(c => subQuery.Contains(c.ContentId));
List myTags=gettagds();
int tagCount=myTags.Count;
可查询子查询=
从myDC.Tags中的标记
其中myTags.Contains(tag.TagId)
按tag.ContentId将tag.TagId分组为g
其中g.Distinct().Count()==tagCount
选择g键;
IQueryable query=myDC.Contents
.Where(c=>subQuery.Contains(c.ContentId));
我还没有测试过这一点,不同的一点可能是关闭了一点。检查生成的sql以确定。

您要执行的操作

LinqToSql可能如下所示:

List<int> myTags = GetTagIds();
int tagCount = myTags.Count;

IQueryable<int> subquery =
  from tag in myDC.Tags
  where myTags.Contains(tag.TagId)
  group tag.TagId by tag.ContentId into g
  where g.Distinct().Count() == tagCount
  select g.Key;

IQueryable<Content> query = myDC.Contents
  .Where(c => subQuery.Contains(c.ContentId));
List myTags=gettagds();
int tagCount=myTags.Count;
可查询子查询=
从myDC.Tags中的标记
其中myTags.Contains(tag.TagId)
按tag.ContentId将tag.TagId分组为g
其中g.Distinct().Count()==tagCount
选择g键;
IQueryable query=myDC.Contents
.Where(c=>subQuery.Contains(c.ContentId));

我还没有测试过这一点,不同的一点可能是关闭了一点。检查生成的sql以确定。

对不起,我应该更清楚一些。问题是,如果我的任务也有一个“bugfix”标记,那么仅仅搜索“portability”和“testing”标记不会带来问题。唯一的问题是你不能这样做。KnowledgeBaseTags上包含(字符串),因为它希望传入KnowledgeBaseTag实体,而不是字符串。我尝试使用
where-tags.All(tag=>t.KnowledgeBaseTaskTags.Any(entity=>entity.tag==tag))
但是这给出了一个异常(除了Contains操作符之外,查询操作符的LINQ-to-SQL实现中不能使用本地序列)。对不起,我应该更清楚一些。问题是,如果我的任务也有一个“bugfix”标记,那么仅仅搜索“portability”和“testing”标记不会带来问题。唯一的问题是你不能这样做。KnowledgeBaseTags上包含(字符串),因为它希望传入KnowledgeBaseTag实体,而不是字符串。我尝试使用
where-tags.All(tag=>t.KnowledgeBaseTaskTags.Any(entity=>entity.tag==tag))
但这给出了一个例外(除了Contains操作符之外,查询操作符的LINQ-to-SQL实现中不能使用本地序列)。这非常有效(必须向子查询添加ToList()并选择()调用主查询,但在其他方面有效。这非常有效(必须向子查询添加ToList()并选择()调用主查询,但在其他方面有效)。