title: Linq To SQL: Most Efficient Way of Looping through List of ID's
body: <p>Say I have a list of ID's:</p>
  
  <pre><code>int[] ids = { 1, 2, 3, 4 };
  </code></pre>
  
  <p>I already have a compiled LINQ statement that returns the individual row (lets call them "Bar" here). So I could just do this:</p>
  
  <pre><code>var foo = new List&lt;Bar&gt;();
  var len = ids.Length;
  for (int i=0; i &lt; len; i++)
  {
      foo.Add(db.GetBarByID(ids[i]));
  }
  </code></pre>
  
  <p>What I'm wondering is, if there's a more efficient way of doing this? There's not that much data being returned (several <code>nvarchar</code> and <code>int</code> columns) per row and the list of ID's can be up to 50.</p>
  
  <p>Update:</p>
  
  <p>I'll elaborate on "GetBarByID". It's a simple LINQ statement which returns "Bar". </p>
  
  <pre><code>class Bar
  {
      public int ID {get; set;}
      public string Name {get;set;}
      public int Age {get;set;}
      public string Blah{get;set;}
  }
  
  IQueryable&lt;Bar&gt; GetBarByID(int ID)
  {
      return db.Bar
              .Where(w =&gt; w.Barid == ID)
              .SelectMany(b =&gt; Othertable.Where(w =&gt; w.barid == b.id), 
                  (b, x) =&gt; new Bar { ID = s.id, Name = s.name, Age = s.age, Blah = x.blah });
  }
  </code></pre>
  
  <p>Side note: By efficient, i mean clean code and performance wise.</p>