What I m missing to implement case insensitive search?
这是我的JS文件。我已经完成了服务器端的所有工作。实现了数据表插件的所有基本功能和一些高级功能。但是搜索功能是区分大小写的。如果我搜索"石油",它只显示石油,而不是石油。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | $(document).ready(function () { var oTable = $('#myDataTable').dataTable({ "bServerSide": true, "sPaginationType":"full_numbers", "sAjaxSource":"/DB/AjaxOil", "bProcessing": true, "Search": { "caseInsensitive": true }, "aoColumns": [ { "sName":"OilId", "aTargets": [0], //Edit column "mData":"OilId", "bSearchable": false, "bSortable": false, "mRender": function (data, type, full) { var id = full[0]; //row id in the first column return""+id+""; } }, {"sName":"CommonName" }, {"sName":"BotanicalName" }, {"sName":"PlantParts" }, {"sName":"Distillation" } ], "columnDefs": [ { "targets": [0], className:"hide_column", "searchable": false } ] }); |
(});
这是我的Ajax函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | public ActionResult AjaxOil(JQueryDataTableParamModel param) { IEnumerable<Oil> allOils = _context.Oils.ToList(); IEnumerable<Oil> filteredOils; if (!string.IsNullOrEmpty(param.sSearch)) { filteredOils = allOils .Where(c => c.CommonName.Contains(param.sSearch) || c.BotanicalName.Contains(param.sSearch) || c.PlantParts.Contains(param.sSearch) || c.Distillation.Contains(param.sSearch)); } else { filteredOils = allOils; } var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]); Func<Oil, string> orderingFunction = (c => sortColumnIndex == 1 ? c.CommonName : sortColumnIndex == 2 ? c.BotanicalName : c.PlantParts); var distillationFilter = Convert.ToString(Request["sSearch_4"]); var commonFilter = Convert.ToString(Request["sSearch_1"]); var botanicalFilter = Convert.ToString(Request["sSearch_2"]); var plantFilter = Convert.ToString(Request["sSearch_3"]); if (!string.IsNullOrEmpty(commonFilter)) { filteredOils = filteredOils.Where(c => c.CommonName.Contains(commonFilter)); } if (!string.IsNullOrEmpty(botanicalFilter)) { filteredOils = filteredOils.Where(c => c.BotanicalName.Contains(botanicalFilter)); } if (!string.IsNullOrEmpty(plantFilter)) { filteredOils = filteredOils.Where(c => c.PlantParts.Contains(plantFilter)); } if (!string.IsNullOrEmpty(distillationFilter)) { filteredOils = filteredOils.Where(c => c.Distillation.Contains(distillationFilter)); } var sortDirection = Request["sSortDir_0"]; if (sortDirection =="asc") filteredOils = filteredOils.OrderBy(orderingFunction); else filteredOils = filteredOils.OrderByDescending(orderingFunction); var displayedOils = filteredOils .Skip(param.iDisplayStart) .Take(param.iDisplayLength); var result = from c in displayedOils select new[] { Convert.ToString(c.OilId), c.CommonName, c.BotanicalName, c.PlantParts, c.Distillation }; return Json(new { sEcho = param.sEcho, iTotalRecords = allOils.Count(), iTotalDisplayRecords = filteredOils.Count(), aaData = result }, JsonRequestBehavior.AllowGet); } |
P.S.数据库有500万行,请从性能角度提出建议。
如Shyju所述,请参阅本帖:不区分大小写"contains(string)"。
这将给你一个全面的想法,什么期待。
以下是文章的一小部分摘录,供您参考:
To test if the string
paragraph contains the stringword (thanks @QuarterMeister)
culture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0 Where
culture is the instance ofCultureInfo describing the language that the text is written in.
此外,我鼓励您访问本文,其中详细比较了各种方法的性能,同时检查字符串中是否出现字符串。这将帮助您决定采用什么方法来获得更好的性能。
http://c c.davelozinski.com/c-sharp/fastest-way-to-check-if-a-string-occurs-within-a-string
根据您的问题,您可能需要创建一个扩展方法,该方法将根据输入类型使用不同的方法进行预执行检查,以获得最佳性能。
希望这有帮助!
首先,您不应该使用