0

i started with this! and i would like to avoid this

private IQueryable<Customer> FilterResult(string search, List<Customer> dtResult, List<string> columnFilters)
        {
            IQueryable<Customer> results = dtResult.AsQueryable();

            results = results.Where(p => (search == null || (p.Name != null && p.Name.ToLower().Contains(search.ToLower()) || p.City != null && p.City.ToLower().Contains(search.ToLower())

                && (columnFilters[0] == null || (p.Name != null && p.Name.ToLower().Contains(columnFilters[0].ToLower())))
                && (columnFilters[1] == null || (p.City != null && p.City.ToLower().Contains(columnFilters[1].ToLower())))

                );

            return results;
        }

by using reflection maybe.... because imagine i have 100 properties it would be easy to mess up ...so i tried this way i would like to use reflection to loop on all properties instead of making references to each of them(properties)

 public IQueryable<Aangifte> FilterColumn<T>()
            {

                try
                {
                    List<Aangifte> v = AangifteGetData.GetData(StartDate, EndDate);
                    List<Aangifte> v2 = new List<Aangifte>();
                    Aangifte Foo = new Aangifte();
                    List<string> propertEntity = new List<string>();

                    var search = Request.Form.GetValues("search[value]").FirstOrDefault();
                    int count = -1;
                    var results = v.AsQueryable();


                    List<string> columnName = new List<string>();
                    foreach (var prop in Foo.GetType().GetProperties())
                    {
                        var t = "";

                        if (!prop.Name.Contains("FORMAT"))
                        {

                             TVALUE = prop.GetValue(Foo, null);
                             t= prop.Name;
                              propertEntity.Add(t);
                              count++;
                        }
                      if (count < propertEntity.Count())
                        {
                            var newt = t;


                            var Z = Request.Form.GetValues("columns[" + count + "][search][value]").FirstOrDefault();




                            results = results.Where
                                       (


                                       p => (search == null || (t != null && t.ToLower().Contains(search.ToLower())))
                                        && (Request.Form.GetValues("columns[" + count + "][search][value]").FirstOrDefault() == null || (t != null && t.ToLower().Contains(Request.Form.GetValues("columns[" + count + "][search][value]").FirstOrDefault().ToLower())))
                                        );

                        }

                         }
                    return results;
                }
                catch (Exception EX)
                {

                    throw EX;
                }
            }
isa
  • 11
  • 7
  • Did you see this [prior question](https://stackoverflow.com/a/6637710/5873815) which seems to be on topic. Also, did you go through [this list](https://stackoverflow.com/search?q=property+value+reflection) before hand ? There seems to be a lot of related questions. – Antry Mar 15 '18 at 15:13
  • If i understand it correctly you're simply getting the value on the wrong place. you should loop through the results. Within the loop you loop through the properties of the result and do the conditional statements. I think it should work then – bramve Mar 15 '18 at 15:16

0 Answers0