数据表与服务器端处理集成,扩展帮助程序以在列表中<T>搜索



我已经成功实现了此助手1,并且可以从开箱即用。我的问题是我正在尝试扩展它以允许搜索列表。

示例。

Class Journalist
  public string Name { get; set; }
  public List<Publication> Publications { get; set; }

在我的DataTables列中

  {
  "width": "25%", "target": 3, "data" : "Publications",
  "render": function (data, type, full, meta) {
         return PublicationLoop(data);
        }
  }
    function PublicationLoop(data) {
      var content = '';
        $.each(data, function(propName, propVal) {
            content += '<a href="/Publication/details/' + propVal.ID + '">' + propVal.Name + '</a>, ';
        });
        return content.substr(0, content.length - 2);;
    }

上面的所有功能都很好,但是助手在出版物列中找不到内容,因为它无法识别类型,但是我可以弄清楚如何在列表的名称字段中修改助手。<<<<<<<<<<<<<<

    public static IQueryable<T> ToGlobalSearchInAllColumn<T>(this IQueryable<T> table, DTParameters Param)
    {
        var GlobalSearchText = Param.Search != null && Param.Search.Value != null ? Param.Search.Value : string.Empty;
        if (!string.IsNullOrEmpty(GlobalSearchText))
        {
            // return BooksData.Where(x => x.BookId.ToString() == GlobalSearchText || x.BookName.Contains(GlobalSearchText) || x.Category.Contains(GlobalSearchText));
            StringBuilder WhereQueryMaker = new StringBuilder();
            Type SearchType = table.FirstOrDefault().GetType();
            DateTime CreatedOn;
            foreach (PropertyInfo prop in SearchType.GetProperties())
            {
                if (prop.PropertyType == typeof(System.String))
                    WhereQueryMaker.Append((WhereQueryMaker.Length == 0 ? "" : " OR ") + prop.Name + ".Contains(@0)");
      // ->    This is the line I'm try to add but the Query causes the app to fail.
                else if (prop.PropertyType == typeof(List<Business.Publication>))
                    WhereQueryMaker.Append((WhereQueryMaker.Length == 0 ? "" : " OR ") + prop.Name + ".Contains(@0)");
                else if (prop.PropertyType == typeof(System.Int32))
                    //if data type is integer then you need to parse to ToString() to use Contains() function
                    WhereQueryMaker.Append((WhereQueryMaker.Length == 0 ? "" : " OR ") + prop.Name + ".ToString().Contains(@0)");
                else if (prop.PropertyType == typeof(System.DateTime?) && DateTime.TryParseExact(GlobalSearchText, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out CreatedOn))
                    //Date object comparison required to follow DateTime(2018,08,15) as format. so need to supply yyyy, MM, dd value on it.
                    WhereQueryMaker.Append((WhereQueryMaker.Length == 0 ? "" : " OR ") + prop.Name + "== DateTime(" + CreatedOn.Year + ", " + CreatedOn.Month + ", " + CreatedOn.Day + ")");
            }
            return table.Where(WhereQueryMaker.ToString(), GlobalSearchText);
        }
        return table;
    }

它引发的错误

'无通用方法'包含' 与提供的类型参数和参数兼容。没有类型 如果该方法是非生成的,则应提供参数。'

SearchType.GetProperties()prop上放一张手表。然后在循环时放一个断点,然后查看存在哪些类型。然后设置您的else if检查出版物列需要的任何内容。

最新更新