0

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. '

Tim Cadieux
  • 447
  • 9
  • 21

1 Answers1

0

Put a watches on SearchType.GetProperties() and prop. Then put a breakpoint while looping and look to see what types are present. Then set your else if check for whatever it needs to be for the Publications column.

cwalvoort
  • 1,851
  • 1
  • 18
  • 19
  • Prop.Name is "Publications", which is the name of the List of Publication – Tim Cadieux May 18 '19 at 15:49
  • what type is associated with the prop? That’s what you need to know. – cwalvoort May 18 '19 at 18:40
  • It looks like you are running the string contains function on the list. You need to instead run the contains function over the items of the list. With Linq, check if any item in the list contains the search string. – cwalvoort May 18 '19 at 18:43
  • This is what I was hoping someone could show me how to do. – Tim Cadieux May 18 '19 at 20:21
  • Try this: https://stackoverflow.com/questions/22913540/dynamic-linq-query-contains-list – cwalvoort May 18 '19 at 21:31
  • I hate to say, I can't make Heads or Tails of what I'm supposed to do next. The before last line, Return Table.where returns this "{Name.Contains(@0) OR Publications.Name.Contains(@0) OR ID.ToString().Contains(@0)}" – Tim Cadieux May 19 '19 at 11:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193583/discussion-between-kdq0-and-tim-cadieux). – cwalvoort May 19 '19 at 12:53
  • I am going to a funeral today and will be travelling tomorrow to go home, so it will be Tuesday. Thx – Tim Cadieux May 19 '19 at 13:22