title: How to write Inline If Statement(SQL IIF) in EFCore Select?
body: <p>I have the following <code>Customer</code> table:</p>
  
  <pre><code>Id  First    Last   LocationId
  0   John     Doe    2
  1   Mary     Smith  4
  </code></pre>
  
  <p>My use case requires column level permissions(predicated on a value in the Entity's table).</p>
  
  <p>How can I query like the following thru EFCore?</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT Id, First, IIF(LocationId in(2), Last, '') FROM Customer;
  </code></pre>
  
  <p>Whereby <code>Last</code> is returned only when <code>LocationId == 2</code>.</p>
  
  <ul>
  <li>Can this be accomplished in Linq-to-Entities as a dynamic type?</li>
  <li>If not, can I use <code>FromSql()</code> and <a href="https://docs.microsoft.com/en-us/ef/core/modeling/query-types" rel="nofollow noreferrer"><code>QueryTypes</code></a>?</li>
  <li>I found this SO <a href="https://stackoverflow.com/questions/53873765/how-to-create-inline-if-statement-with-expressions-in-dynamic-select-for-null">How to create “inline if statement” with expressions in dynamic select for null checking</a>.  But I am not familiar with <code>Expression</code> type.  This implies its possible however.</li>
  </ul>