Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Reuse Linq query

Reuse Linq query

Scheduled Pinned Locked Moved LINQ
databasecsharplinq
4 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hkodavati
    wrote on last edited by
    #1

    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.

    H 1 Reply Last reply
    0
    • H hkodavati

      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.

      H Offline
      H Offline
      Harvey Saayman
      wrote on last edited by
      #2

      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)

      H 1 Reply Last reply
      0
      • H Harvey Saayman

        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)

        H Offline
        H Offline
        hkodavati
        wrote on last edited by
        #3

        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).

        H 1 Reply Last reply
        0
        • H hkodavati

          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).

          H Offline
          H Offline
          Howard Richards
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups