2016-11-15 42 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统计的搜索关键词中的每个列表