title: C# Linq Query on database using field name as a variable
body: <p>Here is my code. Can anyone help me refactor this piece of code. i'm trying to use field name as variable so I wouldn't have to use switch case. Something like from <code>dr in db.Users where dr.FieldName.Contains(value)</code>. </p>
  
  <pre><code>public static List&lt;User&gt; GetUser(string value, string fieldName)
  {
      var db = new CCPTDataContext();
      var users = new List&lt;User&gt;();
  
      switch (fieldName)
      {
          case "FirstName":
              users = (from dr in db.Users where dr.FirstName.Contains(value)
                       select new User
                       {
                           Username = dr.Username,
                           FirstName = dr.FirstName,
                           LastName = dr.LastName
                       }).ToList();
  
              break;
          case "LastName":
              users = (from dr in db.Users where dr.LastName.Contains(value)
                       select new User
                       {
                           Username = dr.Username,
                           FirstName = dr.FirstName,
                           LastName = dr.LastName
                       }).ToList();
              break;
      }
      return users;
  }
  </code></pre>