Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何统计来自数据库的搜索内容_C#_Jquery_Jquery Ui - Fatal编程技术网

C# 如何统计来自数据库的搜索内容

C# 如何统计来自数据库的搜索内容,c#,jquery,jquery-ui,C#,Jquery,Jquery Ui,我找到了搜索词的描述,并使用JQuery autocomplete获得了它。现在我想显示每个描述的搜索词(单词)的计数 如果(!string.IsNullOrEmpty(searchTerm)) { //在TrainingTopic中搜索描述和名称 var modeltrainingtopic=db.TrainingTopicRepository.Get().Where(x=>x.Description.ToLower().Contains(searchTerm.ToLower())| | x

我找到了搜索词的描述,并使用JQuery autocomplete获得了它。现在我想显示每个描述的搜索词(单词)的计数


如果(!string.IsNullOrEmpty(searchTerm))
{
//在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);
//在子主题中搜索描述、内容和名称
var modelsubtopic=db.SubTopicRepository.Get().Where(x=>x.Description.ToLower().Contains(searchTerm.ToLower())| | x.SubTopicContent.ToLower().Contains(searchTerm.ToLower())。选择(x=>newsearchviewmodel{Id=x.Id,IsSubTopic=true,Description=x.Description,Content=x.SubTopicContent,RedirectionLink=“”});
//在子主题中搜索描述、内容和名称
var modelsubsubtopic=db.SubSubTopicRepository.Get().Where(x=>x.Description.ToLower().Contains(searchTerm.ToLower())| | x.Name.ToLower().Contains(searchTerm.ToLower())。选择(x=>newsearchviewmodel{Id=x.Id,IsSubSubTopic=true,Name=x.Name,Description=x.Description,Content=x.Content,RedirectionLink=”“});
返回modeltrainingtopic.Concat(modelssubtopic).Concat(modelsubsubtopic);
}
在我的视图模型中,我添加了StringCount。现在我想添加每个对象(modeltrainingtopic、modelsubtopic、modelsubsubtopic)中搜索项的计数。下面是我添加jquery的方法

$(函数(){
var loc=window.location.pathname.split('/')[1];
$(“#srch term”).autocomplete({
来源:功能(请求、响应){
$.ajax({
url:“/”+loc+“/api/ResourceLanding/SearchString?searchTerm=“+request.term,
键入:“获取”,
成功:功能(数据){
如果(!data.length){
var结果=[
{
id:0,
标签:“未找到匹配项”
}
];
反应(结果);
}
否则{
响应($.map)(数据、功能(项){
返回{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(“实例”)。\u renderItem=函数(ul,项){
var图标;
如果(项目IsTrainingTopic){
图标=“”
}
其他如果(项目发布主题){
图标=“”
}
其他如果(项目发布主题){
图标=“”
}
如果(图标!==未定义){
返回$(“
  • ”) .append(“+图标+”+项目标签+”) .附录(ul); } 否则{ 返回$(“
  • ”) .append(“+item.label+”) .附录(ul); } }; });

  • 请帮帮我

    还有一种方法:

    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; }
    
        }
    
    (二) 创建将返回“SearchTerm”字数的方法,如:

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

    现在创建类“YourListItems”类型的列表对象


    这是我问题的答案

    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,然后计算每个列表中的搜索词数

    谢谢你的回复。但是我必须在创建的每个对象中存储搜索词数。我想知道如何编写代码。如果你有任何想法,请帮助我。我已经编辑了我的答案。请检查。希望它能帮助你。这些方法我们存储为列表bro..in这个列表告诉我们如何在描述中直接找到搜索词?意味着…兄弟你需要从“modeltrainingtopic.description”中计算“搜索词”?Bro,请参阅后端代码。我从3个表中获取了数据。我从描述、名称和内容中获取了searchterm数据。我分别附加了数据行。因此,我需要计数,一行中有多少个搜索项。例如,在方法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,
    
                  }
    
                );
            }
    
    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();