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. Declare query in or out of loop?

Declare query in or out of loop?

Scheduled Pinned Locked Moved LINQ
databasecsharplinqtutorialquestion
2 Posts 2 Posters 0 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.
  • M Offline
    M Offline
    MarkLTX
    wrote on last edited by
    #1

    I need to call the same LINQ to SQL query in a foreach loop, specifying a different value in the 'where' clause each time (i.e. 'someVariable' in the example). Is it better to declare the query inside the loop or outside the loop? I have verified that I get the same results either way. Please ignore the idea of eliminating the outer loop by passing the entire key list to the query. OUTSIDE:

    var someVariable = whatever;
    var q = from rec in table
    where rec.Key = someVariable
    select rec;

    foreach (var key in keylist)
    {
    someVariable = key;
    foreach (var rec in q)
    {
    // Do something.
    }
    }

    INSIDE:

    foreach (var key in keylist)
    {
    var q = from rec in table
    where rec.Key = key
    select rec;

    foreach (var rec in q)
    {
        // Do something.
    }
    

    }

    P 1 Reply Last reply
    0
    • M MarkLTX

      I need to call the same LINQ to SQL query in a foreach loop, specifying a different value in the 'where' clause each time (i.e. 'someVariable' in the example). Is it better to declare the query inside the loop or outside the loop? I have verified that I get the same results either way. Please ignore the idea of eliminating the outer loop by passing the entire key list to the query. OUTSIDE:

      var someVariable = whatever;
      var q = from rec in table
      where rec.Key = someVariable
      select rec;

      foreach (var key in keylist)
      {
      someVariable = key;
      foreach (var rec in q)
      {
      // Do something.
      }
      }

      INSIDE:

      foreach (var key in keylist)
      {
      var q = from rec in table
      where rec.Key = key
      select rec;

      foreach (var rec in q)
      {
          // Do something.
      }
      

      }

      P Offline
      P Offline
      Pranay Rana
      wrote on last edited by
      #2

      i think first option is better one and its working because of linq differed execution take place for more detail :- http://www.codeproject.com/tips/59614/Linq-Deferred-Execution.aspx[^]

      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