jqGridのfilterToolbarを使用してサーバー側の検索/フィルターを実行するOlegの例を実行中です。Nameの検索時にfilteredQuery.Count()行でエラーがスローされていますが、FormatMappingに関連しているようです。「eq」(等しい検索)を実行してもエラーは発生しませんが、「cn」(検索を含む)を実行するとエラーが発生します。フォーマット文字列に問題があるようですが、この正確なコードは、取得したサンプルコードでは正常に機能します。ASP.NETMVC4を使用しています。助けていただければ幸いです。ありがとう!!
public JsonResult DynamicGridData(string sidx, string sord, int page, int rows, bool _search, string filters)
{
var context = new NonTaxContext();
var objectContext = ((IObjectContextAdapter)context).ObjectContext;
var set = objectContext.CreateObjectSet<Team>();
var serializer = new JavaScriptSerializer();
Filters f = (!_search || string.IsNullOrEmpty(filters)) ? null : serializer.Deserialize<Filters>(filters);
ObjectQuery<Team> filteredQuery =
(f == null ? (ObjectQuery<Team>)set : f.FilterObjectSet((ObjectQuery<Team>)set));
filteredQuery.MergeOption = MergeOption.NoTracking; // we don't want to update the data
var totalRecords = filteredQuery.Count();
var pagedQuery = filteredQuery.Skip("it." + sidx + " " + sord, "@skip",
new ObjectParameter("skip", (page - 1) * rows))
.Top("@limit", new ObjectParameter("limit", rows));
// to be able to use ToString() below which does NOT exist in the LINQ to Entity
var queryDetails = (from item in pagedQuery
select new { item.TeamId, item.Code, item.Name }).ToList();
return Json(new
{
total = (totalRecords + rows - 1) / rows,
page,
records = totalRecords,
rows = (from item in queryDetails
select new[] {
item.TeamId.ToString(),
item.Code,
item.Name
}).ToList()
});
}
private static readonly string[] FormatMapping = {
"(it.{0} = @p{1})", // "eq" - equal
"(it.{0} <> @p{1})", // "ne" - not equal
"(it.{0} < @p{1})", // "lt" - less than
"(it.{0} <= @p{1})", // "le" - less than or equal to
"(it.{0} > @p{1})", // "gt" - greater than
"(it.{0} >= @p{1})", // "ge" - greater than or equal to
"(it.{0} LIKE (@p{1}+'%'))", // "bw" - begins with
"(it.{0} NOT LIKE (@p{1}+'%'))", // "bn" - does not begin with
"(it.{0} LIKE ('%'+@p{1}))", // "ew" - ends with
"(it.{0} NOT LIKE ('%'+@p{1}))", // "en" - does not end with
"(it.{0} LIKE ('%'+@p{1}+'%'))", // "cn" - contains
"(it.{0} NOT LIKE ('%'+@p{1}+'%'))" //" nc" - does not contain
};