title: Generic where condition in Entity Framework Core
body: <p>I'm building a Web API application which use Unit of work design pattern. I've created a repository with search function:</p>
  
  <pre><code>public IQueryable&lt;User&gt; Search(SearchUserViewModel parameter)
  {
      // Find all user from database
      var users = GetAll();
  
      // Id has been defined.
      if (parameter.Id != null)
          users = users.Where(x =&gt; x.Id == parameter.Id.Value);
  
      // Email has been defined.
      if (parameter.Email != null &amp;&amp; !string.IsNullOrWhiteSpace(parameter.Email.Text))
      {
          var emailSearch = parameter.Email;
          switch (emailSearch.Mode)
          {
              case TextSearchMode.ContainIgnoreCase:
                  // TODO: Recheck.
                  users = users.Where(x =&gt; x.Email.Contains(emailSearch.Text));
                  break;
              case TextSearchMode.Equal:
                  users = users.Where(x =&gt; x.Email.Equals(emailSearch.Text));
                  break;
              case TextSearchMode.EqualIgnoreCase:
                  users = users.Where(x =&gt; x.Email.Equals(emailSearch.Text, StringComparison.OrdinalIgnoreCase));
                  break;
              default:
                  users = users.Where(x =&gt; x.Email.Contains(emailSearch.Text));
                  break;
          }
      }
  
      if (parameter.Password != null &amp;&amp; !string.IsNullOrWhiteSpace(parameter.Password.Text))
      {
          var passwordSearch = parameter.Password;
          switch (passwordSearch.Mode)
          {
              case TextSearchMode.ContainIgnoreCase:
                  // TODO: Recheck.
                  users = users.Where(x =&gt; x.Email.Contains(passwordSearch.Text));
                  break;
              case TextSearchMode.Equal:
                  users = users.Where(x =&gt; x.Email.Equals(passwordSearch.Text));
                  break;
              case TextSearchMode.EqualIgnoreCase:
                  users = users.Where(x =&gt; x.Email.Equals(passwordSearch.Text, StringComparison.OrdinalIgnoreCase));
                  break;
              default:
                  users = users.Where(x =&gt; x.Email.Contains(passwordSearch.Text));
                  break;
          }
      }
  
      return users;
  }
  </code></pre>
  
  <p>With an enumeration:</p>
  
  <pre><code>public class TextSearch
  {
      /// &lt;summary&gt;
      /// Comparision mode.
      /// &lt;/summary&gt;
      public TextSearchMode Mode { get; set; }
  
      /// &lt;summary&gt;
      /// Text which needs searching.
      /// &lt;/summary&gt;
      public string Text { get; set; }
  }
  </code></pre>
  
  <p>Search function runs fine for now. But I want to reduce my code. I wonder if there is a way to create a function like this one:</p>
  
  <pre><code>private IQueryable&lt;T&gt; SearchPropertyText(IQueryable&lt;T&gt; records, string propertyName, TextSearchMode mode)
  {
      switch (mode)
      {
          case Equal:
              // TODO:
          case EqualIgnoreCase:
              // TODO:
          default:
              // TODO
      }
  
      return records;
  }
  </code></pre>
  
  <p>Then I can use the function like this:</p>
  
  <pre><code>users = SearchPropertyText(users, user.Email, emailSearch.Mode)
  </code></pre>
  
  <p>As you've seen my code above, I have some mode of text searching listed in the TextSearchMode enumeration, base on mode, I'll search property with Equal, EqualIgnoreCase, Contains, ... </p>
  
  <p>I'm thinking about generic function, but I don't know how to create it for now.</p>
  
  <p>Can anyone help me please ?</p>