久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 linq 中檢索標有所有提供的標簽的項目?

How do I retrieve items that are tagged with all the supplied tags in linq?(如何在 linq 中檢索標有所有提供的標簽的項目?)
本文介紹了如何在 linq 中檢索標有所有提供的標簽的項目?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我似乎遇到了這個問題.我有一個帶有 ID 的任務表和一個標簽表,它有一個標簽字段和一個到任務表的外鍵約束.

I seem to be having trouble with this. I have a Task table with an ID, and a Tag table, that has a tag field and a foreign key constraint to the task table.

我希望能夠通過標簽對任務執行 AND 搜索.例如,如果我搜索帶有可移植性"和測試"標簽的任務,我不想要帶有可移植性"而不是測試"標簽的任務.

I want to be able to perform AND searches for tasks by tags. So for example, if I search for tasks with the tags "portability" and "testing", I don't want tasks that are tagged with "portability" and not "testing".

我嘗試了以下語法:

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

這當然是 OR 搜索,而不是 AND 搜索.我不知道如何將其實際切換為 AND 搜索.

This of course does an OR search, not an AND search. I can't figure out how to actually switch this to be an AND search.

編輯我還需要能夠搜索任務包含的 X 個標簽中的 2 個.因此,如果任務被標記為錯誤修復"、可移植性"、測試"并且我搜索測試"和可移植性",該任務仍會顯示.

Edit I also need to be able to search for 2 out of X tags that a task contains. So if the task is tagged with "bugfix", "portability", "testing" and I search for "testing" and "portability", that task will still show up.

推薦答案

你想做的這個

LinqToSql 可能看起來像:

the LinqToSql might look like:

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));

我還沒有測試過這個,Distinct 位可能有點差.檢查生成的 sql 以確保.

I haven't tested this and the Distinct bit might be off a little. Check the generated sql to be sure.

這篇關于如何在 linq 中檢索標有所有提供的標簽的項目?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
主站蜘蛛池模板: 黄色av网站在线观看 | 国产精品一区二区电影 | 久久久久国产一区二区三区四区 | 337p日本欧洲亚洲大胆 | 国产成人jvid在线播放 | 国产精品一区免费 | 日本视频免费 | 日韩福利在线 | 96av麻豆蜜桃一区二区 | 久久精品免费观看 | 国产精品久久久久久久久久 | 久久精彩 | 免费av在线 | 欧美片网站免费 | 国产精品视频专区 | 秋霞a级毛片在线看 | 黄色成人国产 | 美女天天操 | 精品久久久久久久久亚洲 | 午夜免费在线观看 | 久久成人精品视频 | 91精品在线看 | 日韩免费一区 | 欧美在线二区 | 久久婷婷麻豆国产91天堂 | 欧美一级久久精品 | 色女人天堂 | 国产成人精品久久二区二区91 | 久久久久久国 | 久久精品手机视频 | 中文字幕成人网 | 亚洲 欧美 综合 | 久久久久久久久久久一区二区 | 一区二区三区小视频 | 亚洲精品国产综合区久久久久久久 | 福利网址 | 欧美久久久久久 | 国产视频一区二区 | 欧美日韩在线成人 | 男女免费在线观看视频 | 中文字幕在线观看一区 |