title: How can I add OrderBy to this LINQ statement?
body: <p>I'm getting data out of an XML file by sending a where clause as delegate to a custom method:</p>
  
  <pre><code>foreach (PageItem pageItem in GetPageItems(xmlDoc, sf =&gt; (int)sf.Element("id") == id))
  {
      _collection.Add(pageItem);
  }
  </code></pre>
  
  <p>which works fine but now I want to add an <strong>OrderBy</strong> clause as well, but can't get the right syntax, here it <strong>doesn't recognize "pageItem"</strong> in the OrderBy clause, etc. </p>
  
  <p><strong>How can I get OrderBy to work in code below?</strong></p>
  
  <pre><code>public IEnumerable&lt;PageItem&gt; GetPageItems(XDocument xmlDoc, Func&lt;XElement, bool&gt; whereClause)
  {
      var pageItems = xmlDoc.Descendants("pageItem")
          .Where(whereClause)
          .OrderBy((int)pageItem.Element("displayOrder").Value)
          .Select(pageItem =&gt; new PageItem
          {
              Id = (int)pageItem.Element("id"),
              WhenCreated = (DateTime)pageItem.Element("whenCreated"),
              ItemOwner = pageItem.Element("itemOwner").Value,
              PublishStatus = pageItem.Element("publishStatus").Value,
              CorrectionOfId = (int)pageItem.Element("correctionOfId"),
  
              IdCode = pageItem.Element("idCode").Value,
              Menu = pageItem.Element("menu").Value,
              Title = pageItem.Element("title").Value,
              Description = pageItem.Element("description").Value,
              AccessGroups = pageItem.Element("accessGroups").Value,
              DisplayOrder = (int)pageItem.Element("displayOrder")
  
  
          });
      return pageItems;
  }
  </code></pre>