0

I would like to filter my List for Reporting purposes but i would like to make it as dynamic as possible so that the user can Filter on 1 or more columns.

The idea i have is to create a Dictionary and then pass that to a method that will filter out the relevant records but i am now stuck as to how i would do the "Dynamic" part.

This is how my current code looks so far but it is not working.

filters = {CourseId,2},{CourseDescription,Maths}

public IQueryable<Course> Filter(Dictionary<string,string> filters)
{
    var a = from s in context.Courses
                  select s;
    foreach (var filter in filters)
    {
        if(!string.IsNullOrEmpty(filter.Value))
        {
            a = a.Where(s => s./*filter.Key*/.ToUpper().Contains(/*filter.value*/.ToUpper()));
        }
    }
}

Please can someone assist and point me into the correct direction so that i can get this working.

Thank you.

puretppc
  • 3,232
  • 8
  • 38
  • 65
Gremlin1708
  • 91
  • 1
  • 1
  • 13
  • Your `filters` definition does not match `Dictionary` declaration. You don't have any `string` in your code... – MarcinJuraszek Jan 16 '14 at 23:58
  • Perhaps this question is related to what you are trying to do? http://stackoverflow.com/questions/848415/linq-dynamic-where-clause – Mark Jan 17 '14 at 00:00
  • This one written by someone in Microsoft can build IQueryable by parameters of strings, and it is in fact in the Samples along with Visual Studio, namely Dynamic.cs.http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx – Lei Yang Jan 17 '14 at 00:14
  • Thank you for your link Lei Yang. This seems to be the answer for my question : [http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx](http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx) – Gremlin1708 Jan 17 '14 at 06:38

1 Answers1

0

I have just recently used http://www.albahari.com/nutshell/predicatebuilder.aspx to dynamically build a predicate to use with Entity Framework, based on criteria that is received. Works quite well.

Brendan Green
  • 11,676
  • 5
  • 44
  • 76