Я успешно реализовал этот Помощник 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;
}
ошибка, которую он выдает
'Нет универсального метода' Contains 'для типа' System.Linq.Enumerable '
совместим с предоставленными аргументами типа и аргументами. Нет типа
аргументы должны быть предоставлены, если метод не является универсальным. «