c# - Expression is not filtering immediately -
please see sample below. linq expression asking list of student objects _age in between 15 , 20. when see varlist in debugger, shows 5 entries. when print varlist, prints 1 entry, correct. after printing, debugger shows 5 entries in varlist. behavior documented where, please let me know. should there lot of entries in list, millions, linq expression filter 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 = rv in studentlist rv.age > 15 && rv.age < 20 select rv; foreach (student x in varlist) { console.writeline("{0} {1}", x.age, x.studentname); } }
this how deferred execution works linq. linq query has expression tree behind, can @ linq query data structure. query not execute until request data, when expression compiled, happens when enumerating through it.
you can read more here:
http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx
ienumerable<customer> query = customer in db.customers << query customer.city == "paris" << not execute select customer; << here foreach (var customer in query) << query executes here
no matter using ienumerable or iqueryable represent query variable, both going use deferred execution:
Comments
Post a Comment