Please see the sample below. LINQ expression is asking for the list of Student objects with _Age in between 15 and 20. When I see varlist in debugger, it shows 5 entries. When I print varlist, it prints just one entry, which is correct. Even after Printing, debugger shows 5 entries in varlist. Is this behavior documented any where, please let me know. Should there be lot of entries in the list, like millions, for LINQ expression to actually filter the items.
public class Student
{
public int StudentID { get; set; }
public String StudentName { get; set; }
public int Age { get; set; }
}
static void Main(string[] args)
{
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 }
};
IEnumerable<Student> varlist = from rv in studentList
where rv.Age > 15 && rv.Age < 20
select rv;
foreach (Student x in varlist)
{
Console.WriteLine("{0} {1}", x.Age, x.StudentName);
}
}