jeudi 15 mars 2018

how can i get property of an entity if it's a generic

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

  public IQueryable<Aangifte> FilterAangiftebis()
                {
                    Aangifte foo = new Aangifte();
                    Dictionary<string,object> column = new Dictionary<string, object>();
                    var search = Request.Form.GetValues("search[value]").FirstOrDefault();
                    foreach (var prop in foo.GetType().GetProperties())
                    {
                        column.Add(prop.Name, prop.GetValue(foo, null));
                        Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(foo, null));
                    }
                    int count=0;
        ///this brings data of type Aangifte
                    List<Aangifte> v = AangifteGetData.GetData(StartDate, EndDate);
                    var results = v.AsQueryable();
                    foreach (var item in column)
                    {
                        var t = Request.Form.GetValues("columns[" + count + "][search][value]").FirstOrDefault();

                        results = results.Where
                     (
                       p => (search == null || column[item.Key].ToString() != null && column[item.Key].ToString().ToLower().Contains(search.ToLower())))



                        && (column[item.Key]==null || (column[item.Key].ToString() != null && column[item.Key].ToString().Contains.Contains(item.ToString().ToLower())));
                    }
        }





Aucun commentaire:

Enregistrer un commentaire