title: Foreach with where clause when reading from a Sharepoint List
body: <p>I am trying to filter a result set which I read from a Sharepoint(list). I have to filter records that have a user specified and sadly user column of the list is a lookup column and gets the actual username from another list.</p>
  <p>Normally,I can get the username value of a record (if it has any) like this:</p>
  <pre><code>foreach (var item in listItems)
  {
      if (item.FieldValues.TryGetValue(&quot;nqhs&quot;, out var userLookup))
      {
          if (userLookup != null &amp;&amp; userLookup.ToString() == &quot;Microsoft.SharePoint.Client.FieldUserValue&quot;)
          {
              var theUser = ((FieldLookupValue)item[&quot;nqhs&quot;]).LookupValue; //this returns theUser = &quot;John Doe&quot;
          }
      }
  }
  </code></pre>
  <p><em>nqhs is the name of the column which holds the Username in the Sharepoint list.</em></p>
  <p>Is there any way I can apply this logic to a where clause in foreach, so that I can work with a much smaller result set?</p>
  <p>I am trying something like this but I can't get it to work:</p>
  <pre><code>foreach (var item in listItems.Where(p =&gt; p.((FieldLookupValue)p[&quot;nqhs&quot;]).LookupValue == &quot;John Doe&quot;))
  </code></pre>
  <p>Also tried this and get and &quot;object reference not set to an instance of an object&quot; error...</p>
  <pre><code>foreach (var item in from item in listItems where ((FieldLookupValue)item[&quot;nqhs&quot;]).LookupValue == &quot;John Doe&quot; select item)
  </code></pre>