2016-11-15 30 views
2

我找到了搜索詞的描述,並使用JQuery自動填充獲得了該詞。現在我想爲每個描述顯示搜索詞(單詞)的數量。如何計算來自db的搜索內容

<strike> 
if (!string.IsNullOrEmpty(searchTerm)) 
     { 
      //searching description and name in TrainingTopic 
      var modeltrainingtopic = db.TrainingTopicRepository.Get().Where(x => x.Description.ToLower().Contains(searchTerm.ToLower()) || x.Name.ToLower().Contains(searchTerm.ToLower())).Select(x => new SearchViewModel { Id = x.Id, IsTrainingTopic=true, Description = x.Description, Name = x.Name, RedirectionLink = "" }).Take(10).ToList(); 

      //searching description, content and name in SubTopic 
      var modelsubtopic = db.SubTopicRepository.Get().Where(x => x.Description.ToLower().Contains(searchTerm.ToLower()) || x.Name.ToLower().Contains(searchTerm.ToLower()) || x.SubTopicContent.ToLower().Contains(searchTerm.ToLower())).Select(x => new SearchViewModel { Id = x.Id,IsSubTopic=true, Description = x.Description, Content = x.SubTopicContent, RedirectionLink = "" }).Take(10).ToList(); 

      //searching description, content and name in SubSubTopic 
      var modelsubsubtopic = db.SubSubTopicRepository.Get().Where(x => x.Description.ToLower().Contains(searchTerm.ToLower()) || x.Name.ToLower().Contains(searchTerm.ToLower()) || x.Content.ToLower().Contains(searchTerm.ToLower())).Select(x => new SearchViewModel { Id = x.Id,IsSubSubTopic=true, Name = x.Name, Description = x.Description, Content = x.Content, RedirectionLink = "" }).Take(10).ToList(); 
      return modeltrainingtopic.Concat(modelsubtopic).Concat(modelsubsubtopic); 
     } 
</strike> 

在我的視圖模型中,我已經添加了StringCount。現在我想在每個對象(modeltrainingtopic,modelsubtopic,modelsubsubtopic)中添加搜索項的計數。下面我怎麼添加的jQuery

$(function() { 

    var loc = window.location.pathname.split('/')[1]; 
    $("#srch-term").autocomplete({ 
     source: function (request, response) { 

      $.ajax({ 
       url: "/" + loc + "/api/ResourceLanding/SearchString?searchTerm=" + request.term, 
       type: "Get", 
       success: function (data) { 
        if (!data.length) { 
         var result = [ 
         { 
          id: 0, 
          label: 'No matches found' 
         } 
         ]; 
         response(result); 
        } 
        else { 
         response($.map(data, function (item) { 
          return { label: item.Description, value: item.Description, Id:item.Id, IsTrainingTopic: item.IsTrainingTopic, IsSubTopic: item.IsSubTopic, IsSubSubTopic: item.IsSubSubTopic, Name: item.Name, Content: item.Content }; 
         })) 
        } 
       } 
      }) 
     } 
    }).autocomplete("instance")._renderItem = function (ul, item) { 
     var icon; 
     if (item.IsTrainingTopic) { 
      icon = '<i class="fa fa-globe" aria-hidden="true"></i>' 
     } 
     else if (item.IsSubTopic) { 
      icon = '<i class="fa fa-cog" aria-hidden="true"></i>' 
     } 
     else if (item.IsSubTopic) { 
      icon = '<i class="fa fa-cogs" aria-hidden="true"></i>' 
     } 
     if (icon !== undefined) { 
      return $("<li>") 
       .append("<div style='width:100%' data-id=" + item.value + ">" + icon + " " + item.label + "</div>") 
       .appendTo(ul); 
     } 
     else { 
      return $("<li>") 
      .append("<div style='width:100%' data-id=" + item.value + ">" + item.label + "</div>") 
      .appendTo(ul); 
     } 
    }; 
}); 

請幫我

回答

2

冗長,而且另一種方式:

1)

只需創建與您將獲得屬性的類你主表中的「modeltrainingtopic」如下:

public class YourListItems 
    { 
     public int Id { get; set; } 
     public bool IsTrainingTopic { get; set; } 
     public string Description { get; set; } 
     public string Content { get; set; } 
     public string Name { get; set; } 
     public string RedirectionLink { get; set; } 
     public int SearchCount { get; set; } 

    } 

2) 創建方法將返回「搜索關鍵詞」字數,如:

static int CountWords(string StringInWhichYouNeedToSearch,string SearchTerm) 
{ 
     return Regex.Matches(StringInWhichYouNeedToSearch, SearchTerm).Count; 
} 

3)

現在創建類的列表對象「YourListItems」型。

 List<YourListItems> myFinalList = new List<YourListItems>(); 

4)

爲您從表「modeltrainingtopic」

 foreach (var SingleRow in modeltrainingtopic) 
     { 

//It will count search term in your description , name and content 

int SearchCount = CountWords(SingleRow.Description, searchTerm) + CountWords(SingleRow.Name, searchTerm) + CountWords(SingleRow.Content, searchTerm); 



//Will add row to new list object named myFinalList 
      myFinalList.Add(

       new YourListItems 
       { 
        Id = SingleRow.Id, 
        IsTrainingTopic = SingleRow.IsTrainingTopic, 
        Description = SingleRow.Description, 
        Content = SingleRow.Content, 
        Name = SingleRow.Name, 
        RedirectionLink = SingleRow.RedirectionLink, 
        SearchCount = SearchCount, 

       } 

      ); 
     } 
+0

感謝您的答覆。但我必須在每個創建的對象中存儲搜索詞計數。我想知道如何編寫代碼。請幫助我,如果你有任何想法 – Sriram

+0

我已編輯我的答案。請檢查。希望它會幫助你。 –

+0

我們將這些方法作爲列表存儲在列表中。如何在列表中直接找到搜索詞? – Sriram

0

及彼表的每一行現在迭代循環是我的問題的答案。

var topicData= db.TrainingTopicRepository.Get().Where(x => x.Description.ToLower().Contains(searchTerm.ToLower()) || x.Name.ToLower().Contains(searchTerm.ToLower())).Select(x=>x).Take(10).ToList(); 
var modeltrainingtopic = topicData.Select(x => new SearchViewModel { Id = x.Id,StringCount= topicData.Where(s =>s.Id==x.Id&& s.Description.ToLower().Contains(searchTerm.ToLower())).Count()+ topicData.Where(s => s.Id == x.Id && s.Name.ToLower().Contains(searchTerm.ToLower())).Count(), IsTrainingTopic = true, Description = x.Description, Name = x.Name, RedirectionLink = "" }).ToList(); 

在這裏,我把舊的名單,然後比較,我的ID統計的搜索關鍵詞中的每個列表