如何计算来自db的搜索内容

时间:2016-11-15 07:46:55

标签: c# jquery jquery-ui

我找到了搜索词的描述,并使用JQuery自动完成功能获得了该描述。现在我想显示每个描述的搜索词(字)的计数。

<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 个答案:

答案 0 :(得分:2)

冗长但也有另一种方式:

1。)

只需创建一个具有属性的类,您将在主列表中获得&#34; modeltrainingtopic&#34;从表格如下:

    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)。 创建将返回&#34; SearchTerm&#34;的方法字数如:

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

3。)

现在创建类&#34; YourListItems&#34;的列表对象。类型。

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

4。)

现在为表格中的每一行列表迭代循环&#34; modeltrainingtopic&#34;

        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,

              }

            );
        }

答案 1 :(得分: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然后计算每个列表中的searchterm