I have a situation where I have a generic method which accepts an object of type generic and I want to write a LINQ query on that object.
Here is an example:
Generic Method:
public static void GetNonNonVerifiedPersons<TResult>(Person<TResult> model)
{
// How To Write LINQ Here to get non verified person
}
Student Class:
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsVerified { get; set; }
}
Teacher Class:
public class Teacher
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsVerified { get; set; }
}
Person Class:
public class Person<T>
{
public List<T> PersonList { get; set; }
}
Main Class:
// 1. Get Non Verified Students
var persons = new Person<Student>();
var students = new List<Student>()
{
new Student { Id = 1, Name = "Student_A", IsVerified = true },
new Student { Id = 2, Name = "Student_B", IsVerified = false },
};
persons.PersonList = new List<Student>();
persons.PersonList.AddRange(students);
GetNonNonVerifiedPersons(persons);
// 2. Get Non Verified Teachers
var persons2 = new Person<Teacher>();
var teachers = new List<Teacher>()
{
new Teacher { Id = 1, Name = "Teacher_A", IsVerified = true },
new Teacher { Id = 2, Name = "Teacher_B", IsVerified = false },
new Teacher { Id = 3, Name = "Teacher_C", IsVerified = false },
};
persons2.PersonList = new List<Teacher>();
persons2.PersonList.AddRange(teachers);
GetNonNonVerifiedPersons(persons2);
var result = persons.PersonList.Where(x => !x.IsVerified).ToList();