c# - Performance of IQueryable -
i needed write dynamic query on customer database obtaining few fields of customer.
following code
[route("api/getbasiccustlist/{argtype}/{argvalue}")] [httpget] [authorize] public dynamic getcustomerdatausername(string argtype, string argvalue) { iqueryable<customerdto> query = (from recordset in db.customers select new customerdto { companyid = recordset.company.id, contactnum = recordset.contactnum, username = recordset.username, emailaddress = recordset.email, fullname = recordset.fullname, accountnumber = recordset.rcustid } ); switch (argtype) { case "username" : query = query.where(c => c.username.startswith(argvalue)); break; case "contactnum": long mobnum = int64.parse(argvalue); query = query.where(c => c.contactnum == mobnum); break; case "fullname": query = query.where(c => c.fullname.contains(argvalue)); break; } return new { data = query.tolist() }; }
this works fine , solving purpose.
my question here when write first part of query
all customer records , later on apply condition dynamically results brought in memory or complete query generated , executed @ db in 1 shot?
since have 500 records of now, not able find performance lag when take production dealing @ least 200,000 300,000 records.
ok, answer
the query won't executed until reach "tolist" @ end of method
from msdn link shared @georgpatscheider mentioned
at point query expressions executed can vary. linq queries executed when query variable iterated over, not when query variable created. called deferred execution
deferred execution enables multiple queries combined or query extended. when query extended, modified include new operations, , eventual execution reflect changes.
its written if queries have of average
, count
, first
, or max
perform immediate execution.
thanks.
Comments
Post a Comment