Reuse Linq query
-
Hi, Can anyone tell me how i can reuse the following query var query = from s in db.Suppliers select s; I need to reuse this variable query in a button clicked event.
if i understand you correctly you dont have to manually reuse the query... the query expression is executed every time you work with your var, so if something has changed in that suppliers table after the query expression executes the new stuff will show up when you work with your var. Hope that makes sense. Good luck
Harvey Saayman - South Africa Junior Developer .Net, C#, SQL
you.suck = (you.passion != Programming)
-
if i understand you correctly you dont have to manually reuse the query... the query expression is executed every time you work with your var, so if something has changed in that suppliers table after the query expression executes the new stuff will show up when you work with your var. Hope that makes sense. Good luck
Harvey Saayman - South Africa Junior Developer .Net, C#, SQL
you.suck = (you.passion != Programming)
-
Hi, If i write my var query in my form load event and then in the button click i want to navigate through the next 10 records through my button click can i just simply say query.skip(10).take(10).
Firstly.. remember that ASP.NET does not persist the data between creating the page and a postback..you can store variables on a page but they are not persisted on the server.* user requests page -> code is executed -> html returned -> discarded
- user clicks button -> code is executed -> html returned -> discarded
Hence there is no point in writing the var query and storing it - this is the wrong approach with ASP.NET. Secondly the query "var query = from s in db.Suppliers select s;" is a query definition, and NOT a result set. LINQ to SQL does not execute a query until you ask it to do something with it, such as bind it to the control(s) or enumerate it. So the answer is "you don't need to" - just write the query in place as needed, e.g. void ShowData(int startRow) { var db = new DataContext(); var query = from s in db.Suppliers skip 0startRow take pageSize select s; this.Gridview1.DataSource = query; } Hope this helps
'Howard
- user clicks button -> code is executed -> html returned -> discarded