2012-07-18 44 views
0

我想找到一種方法將可選字符串列表傳遞給查詢。我想要做的是通過它們之間的關係過濾標籤列表。例如,如果選擇了c#,我的程序只會建議出現在具有ac#標籤的文檔中的標籤,然後選擇下一個(比如說SQL),將會顯示鏈接到這兩個標籤文檔的標籤,然後削減它以便用戶能夠更接近他的目標。在實體框架中的查詢中使用列表

目前我所擁有的是:

List<Tag> _tags = (from t in Tags 
        where t.allocateTagDoc.Count > 0 
        select t).ToList(); 

這是一個將作爲選擇標籤可反覆使用可選的ARGS調用的方法。


我想我一直在屁股後面。如果我爲每個提供的標籤創建兩個(或更多)查詢,找到它們全部出現在一起的文檔,然後列出與它們一起出現的所有標籤......或者是否會在分貝上點擊太多?我可以完全通過實體上下文變量來完成,只需查詢模型?

再次感謝您的幫助!

回答

0

想到我會彈回,並分享我的解決方案是什麼,我先在腦子裏想的完全不同。

首先,我修改了數據庫,稍微擺脫了allocateDocumentTag表中的無用字段,這使得我可以更有效地使用實體框架模型,因爲我允許我將該表離開並純粹通過Tag和文件。

當我第一次填寫表單時,我只是顯示與文檔有關係的所有標籤。之後使用我的搜索過濾器,當在checkedListBox中選擇標籤時,與該標籤相關聯的文檔ID將被返回,然後被反饋回來以填充使用過的標籤列表框。

public static List<Tag> fillUsed(List<int> docIds = null) 
     { 
      List<Tag> used = new List<Tag>(); 

      if (docIds == null || docIds.Count() < 1) 
      { 
       used = (from t in frmFocus._context.Tags 
         where t.Documents.Count >= 1 
         select t).ToList(); 
      } 
      else 
      {  
       used = (from t in frmFocus._context.Tags 
         where t.Documents.Any(d => docIds.Contains(d.id)) 
         select t).ToList(); 
      } 
      return used; 
     } 

從那裏標籤進入文檔搜索,反之亦然。希望這可以幫助別人,如果答案不清楚,或者你需要更多的代碼,然後留下評論,我會嘗試和排序它。

1

你可以試試這個。

首先收集標籤在字符串列表中搜索。

List<string> tagStrings = new List<string>{"c#", "sql"}; 

通過此列表中查詢,檢查它是否爲空,如果是空的,它會返回所有的標籤,其中tagStrings匹配else標記。

var _tags = (from t in Tags 
      where t.allocateTagDoc.Count > 0 
      && (tagStrings.Count ==0 || tagStrings.Contains(t.tagName)) 
      select t).ToList(); 
+0

非常感謝,我會給它一個。 – 2012-07-18 06:11:05

+0

如果不是字符串,我會從標籤中傳入ID,會更容易嗎? – 2012-07-18 06:27:58

+0

是的,你可以在那裏傳遞ID,Ids會在數據庫查詢中完成預製。 – 2012-07-18 06:41:06

1

你也可以試試這個,字典表示文檔的ID與它的標籤:

 Dictionary<int, string[]> documents = 
      new Dictionary<int, string[]>(); 

     documents.Add(1, new string[] { "C#", "SQL", "EF" }); 
     documents.Add(2, new string[] { "C#", "Interop" }); 
     documents.Add(3, new string[] { "Javascript", "ASP.NET" }); 
     documents.Add(4, new string[] { }); 

     // returns tags belonging to documents with IDs 1, 2 
     string[] filterTags = new string[] { "C#" }; 
     var relatedTags = GetRelatedTags(documents, filterTags); 
     Debug.WriteLine(string.Join(",", relatedTags)); 

     // returns tags belonging to document with ID 1 
     filterTags = new string[] { "C#", "SQL" }; 
     relatedTags = GetRelatedTags(documents, filterTags); 
     Debug.WriteLine(string.Join(",", relatedTags)); 

     // returns tags belonging to all documents 
     // since no filtering tags are specified 
     filterTags = new string[] { }; 
     relatedTags = GetRelatedTags(documents, filterTags); 
     Debug.WriteLine(string.Join(",", relatedTags)); 


    public static string[] GetRelatedTags(
     Dictionary<int, string[]> documents, 
     string[] filterTags) 
    { 
     var documentsWithFilterTags = documents.Where(o => 
      filterTags 
       .Intersect(o.Value).Count() == filterTags.Length); 

     string[] relatedTags = new string[0]; 

     foreach (string[] tags in documentsWithFilterTags.Select(o => o.Value)) 
      relatedTags = relatedTags 
       .Concat(tags) 
       .Distinct() 
       .ToArray(); 

     return relatedTags; 
    } 
+0

事情是我正在經歷一個已經分配標籤(或將由用戶分配)的現有數據庫。雖然字典絕對是一個好主意。謝謝! – 2012-07-18 15:44:36

+1

不客氣,我很樂意提供幫助。 – 2012-07-18 18:43:25