title: How to find one value from an IQueryable in C# ASP.NET MVC 4 using Linq?
body: <p>This is in C# ASP.NET MVC 4 using EF. I have a table <code>Users</code> which has <code>Portfolio</code> object defined as one of its properties and <code>Username</code> as another. The <code>User.Identity.Name</code> in Razor lets me pass the username along to my controller once the person is logged in. A <code>User</code> has a <code>Portfolio_Id</code> in the table from the <code>Portfolio</code> object. What I am trying to do is obtain the <code>Portfolio_Id</code> of a <code>User</code> when all I have to go buy is the <code>Username</code>. In the code below you see I have a <code>var protfolioId</code>. My problem is that it is getting set as an <code>IQueryable&lt;int&gt;</code> from my LINQ when I need it to just be and int. I may be wrong but does that mean it is a collection and not a single value? How can I get just the <code>Portfolio_Id</code> for the <code>User</code> whose <code>Username</code> matched what I passed into the action?</p>
  <pre><code>public ActionResult Index([Bind(Prefix=&quot;id&quot;)]string userName)
      {
          //find the user
          var user = _db.Users
                     .Where(u =&gt; u.UserName == userName)
                     .Select(u =&gt; new User
                     {
                         Id = u.Id,
                         UserName = u.UserName,
                         FirstName = u.FirstName,
                         LastName = u.LastName,
                         Email = u.Email,
                         Portfolio = u.Portfolio
                     });
          //find the users portfolio id
          var portfolioId = user.Select(u =&gt; u.Portfolio.Id);
  
          //find their portfolio
          var portfolio = _db.Portfolios.Find(portfolioId);
          if(portfolio == null)
          {
              return HttpNotFound();
          }//end of if
          return View(portfolio);
      }//end of Index
  </code></pre>