If you dynamic change where predicate in LINQ, you can use Expression.
The Expression only accepts a single line expression.
using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;
public class Program
{
public static void Main()
{
// Student collection
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,
new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,
new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 }
};
Expression<Func<Student, bool>> expr = i => i.Age < 20;
IQueryable<Student> studentQ = studentList.AsQueryable();
var filteredResult = studentQ.Where(expr);
foreach (var std in filteredResult)
Console.WriteLine(std.StudentName);
}
}
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
}