这是我的js文件。我已经在服务器端完成了所有工作。并实现了Datatable插件的所有基本功能和一些高级功能。但是搜索功能区分大小写。如果我搜索“机油”,则仅显示机油,而不显示机油。
$(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 "<a>"+id+"</a>";
}
},
{ "sName": "CommonName" },
{ "sName": "BotanicalName" },
{ "sName": "PlantParts" },
{ "sName": "Distillation" }
],
"columnDefs": [
{
"targets": [0],
className: "hide_column",
"searchable": false
}
]
});
});
这是我的ajax函数
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万行,因此也请从性能的角度提出建议。
答案 0 :(得分:1)
首先,您不应该使用_context.Oils.ToList();
,然后在过滤它们之前将从数据库中检索所有记录。如果将ToList()
放在.Take(param.iDisplayLength)
之后,所有选择代码都将转换为数据库中的查询,并且仅检索相关记录。由Linq扩展执行的Contains(区分大小写;另请参见this SO问题)与我们将由实体框架转换为LIKE SQL语句的Contains(请参见this SO问题)有所不同和this有关跟踪由您的代码生成的SQL语句的视频。
答案 1 :(得分:1)
如Shyju所述,请参阅此帖子: Case insensitive 'Contains(string)'
这将使您对期望有一个全面的想法。
以下是该帖子的一小段摘录供您参考:
测试字符串
paragraph
是否包含字符串word
(感谢@QuarterMeister)
culture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0
culture
是CultureInfo
的实例,描述了文本所用的语言。
此外,建议您访问本文,其中比较了各种方法的性能,同时检查了字符串中是否出现了字符串。这应该可以帮助您决定采用哪种方法以获得更好的性能。
http://cc.davelozinski.com/c-sharp/fastest-way-to-check-if-a-string-occurs-within-a-string
根据您的问题,您可能必须创建一个扩展方法,该扩展方法将使用不同的方法根据输入的类型进行瓶坯检查,以实现最佳性能。
希望这会有所帮助!