I've successfully implemented this Helper 1 and it works fine out of the box. My issue is i'm trying to extend it to allow Search of List.
Example.
Class Journalist
public string Name { get; set; }
public List<Publication> Publications { get; set; }
In my DataTables columns
{
"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);;
}
The above all works fine but the Helper does not find the content in the Publications Column because it doesn't recognize the type but I can;t figure out how to amend the Helper to Search in the Name field of List.
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;
}
The error it throws
'No generic method 'Contains' on type 'System.Linq.Enumerable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic. '