title: Using First() get the 2nd item of LINQ result?
body: <p>I'm new to Linq and EntityFramework. This is a sample program I met while learning them.</p>
  
  <p>The data in table is like this:</p>
  
  <pre><code>BlogId  Title
  1       Hello Blog
  2       New Blog
  3       New Blog
  </code></pre>
  
  <p>I have the following Linq code, trying to read the first blog id(expected to be 2):</p>
  
  <pre><code>var name = "New Blog";
  var blogs = (from b in db.Blogs
                  where b.Title == name
                  orderby b.Title
                  select b);//.ToList();
  
  Console.Write("The first id: ");
  Console.WriteLine(blogs.First().BlogId);
  </code></pre>
  
  <p>The result comes out to be 3.</p>
  
  <p>Then I use <em>ToList()</em>:</p>
  
  <pre><code>var blogs = (from b in db.Blogs
                  where b.Title == name
                  orderby b.Title
                  select b).ToList();
  Console.Write("The first id: ");
  Console.WriteLine(blogs.First().BlogId);
  </code></pre>
  
  <p>The result comes out to be 2.</p>
  
  <p>Can anyone help to explain this? Or is this a bug?</p>
  
  <p>Thanks.</p>
  
  <p>//////////////////////// <strong>UPDATE</strong> /////////////////////////////</p>
  
  <p>I just deleted the data in the database and inserted some new items. Now the table is like this:</p>
  
  <pre><code>BlogId  Title
  5       New Blog
  6       New Blog
  7       New Blog
  8       New Blog
  </code></pre>
  
  <p>Then I ran the program above(Not with ToList()), the First() method returns the id <strong>6</strong>
  So I assume the method always returns the <strong>2nd</strong> item in the situation above. And it doesn't seem to have anything to do with the RDBMS. Can anyone explain?</p>
  
  <p>Thanks.</p>
  
  <p>/////////////////////////////////////////////////////</p>
  
  <p>FYI, the following is the whole .cs file:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  using System.Data.Entity;
  using System.ComponentModel.DataAnnotations;
  
  namespace SampleNew
  {
      class Program
      {
          public class Blog
          {
              [Key]
              public Int32 BlogId { get; set; }
              public String Title { get; set; }
              public virtual List&lt;Post&gt; Posts { get; set; } 
          }
  
          public class Post
          {
              [Key]
              public Int32 PostId { get; set; }
              public String Title{ get; set; }
  
              public String Content { get; set; }
          }
  
          public class BlogContext : DbContext
          {
              public DbSet&lt;Blog&gt; Blogs{ get; set; }
  
              public DbSet&lt;Post&gt; Posts { get; set; }
          }
  
          static void Main(string[] args)
          {
              using (var db = new BlogContext())
              {
                  // Create and save a new Blog 
                  // Console.Write("Enter a name for a new Blog: ");
                  var name = "New Blog";
  
                  //var blog = new Blog { Title = name };
                  var blogs = (from b in db.Blogs
                               where b.Title == name
                               orderby b.Title
                               select b).ToList();
  
                  Console.Write("The first id: ");
                  Console.WriteLine(blogs.First().BlogId);
  
                  Console.WriteLine(blogs.Count());
  
                  Blog blog = null;
                  foreach (Blog b in blogs)
                  {
                      blog = b;
                      Console.WriteLine(blog.BlogId);
                  }
                  Console.WriteLine(blog.BlogId);
  
                  Console.WriteLine(blogs.First().BlogId);
                  Console.WriteLine(blogs.First().BlogId);
  
                  Console.WriteLine(blogs.Last().BlogId);
                  Console.WriteLine(blogs.Last().BlogId);
  
  
                  blog.Posts = new List&lt;Post&gt;();
                  var post = new Post { Content = "Test Content2", Title = "Test Title2"};
                  blog.Posts.Add(post);
  
                  db.Posts.Add(post);
                  db.SaveChanges();
  
                  // Display all Blogs from the database 
                  var query = from b in db.Blogs
                              orderby b.Title
                              select b;
  
                  Console.WriteLine("All blogs in the database:");
                  foreach (var item in query)
                  {
                      Console.WriteLine(item.Title);
                  }
  
                  Console.WriteLine("Press any key to exit...");
                  Console.ReadKey();
              } 
          }
      }
  }
  </code></pre>