title: LINQ select property by name
body: <p>I'm attempting to use a variable inside of a LINQ select statement. </p>
  
  <p>Here is an example of what I'm doing now. </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using Faker;
  
  namespace ConsoleTesting
  {
  internal class Program
  {
      private static void Main(string[] args)
      {
          List&lt;Person&gt; listOfPersons = new List&lt;Person&gt;
          {
              new Person(),
              new Person(),
              new Person(),
              new Person(),
              new Person(),
              new Person(),
              new Person(),
              new Person(),
              new Person(),
              new Person(),
              new Person()
          };
  
          var firstNames = Person.GetListOfAFirstNames(listOfPersons);
  
          foreach (var item in listOfPersons)
          {
              Console.WriteLine(item);
          }
  
          Console.WriteLine();
          Console.ReadKey();
      }
  
  
      public class Person
      {
          public string City { get; set; }
          public string CountryName { get; set; }
          public string FirstName { get; set; }
          public string LastName { get; set; }
  
          public Person()
          {
              FirstName = NameFaker.Name();
              LastName = NameFaker.LastName();
              City = LocationFaker.City();
              CountryName = LocationFaker.Country();
          }
  
          public static List&lt;string&gt; GetListOfAFirstNames(IEnumerable&lt;Person&gt; listOfPersons)
          {
              return listOfPersons.Select(x =&gt; x.FirstName).Distinct().OrderBy(x =&gt; x).ToList();
          }
  
          public static List&lt;string&gt; GetListOfCities(IEnumerable&lt;Person&gt; listOfPersons)
          {
              return listOfPersons.Select(x =&gt; x.FirstName).Distinct().OrderBy(x =&gt; x).ToList();
          }
  
          public static List&lt;string&gt; GetListOfCountries(IEnumerable&lt;Person&gt; listOfPersons)
          {
              return listOfPersons.Select(x =&gt; x.FirstName).Distinct().OrderBy(x =&gt; x).ToList();
          }
  
          public static List&lt;string&gt; GetListOfLastNames(IEnumerable&lt;Person&gt; listOfPersons)
          {
              return listOfPersons.Select(x =&gt; x.FirstName).Distinct().OrderBy(x =&gt; x).ToList();
          }
      }
  }
  }
  </code></pre>
  
  <p>I have a Some very not DRY code with the GetListOf...   Methods </p>
  
  <p>i feel like i should be able to do something like this </p>
  
  <pre><code>public static List&lt;string&gt; GetListOfProperty(
  IEnumerable&lt;Person&gt; listOfPersons, string property)
          {
              return listOfPersons.Select(x =&gt;x.property).Distinct().OrderBy(x=&gt; x).ToList();
          }
  </code></pre>
  
  <p>but that is not vaild code.   I think the key Might Relate to Creating a Func </p>
  
  <p>if That is the answer how do I do that?</p>
  
  <p>Here is a second attempt using refelection But this is also a no go. </p>
  
  <pre><code>        public static List&lt;string&gt; GetListOfProperty(IEnumerable&lt;Person&gt; 
  listOfPersons, string property)
          {
              Person person = new Person();
              Type t = person.GetType();
              PropertyInfo prop = t.GetProperty(property);
              return listOfPersons.Select(prop).Distinct().OrderBy(x =&gt; 
  x).ToList();
  }
  </code></pre>
  
  <p>I think the refection might be a DeadEnd/red herring but i thought i would show my work anyway.           </p>
  
  <p>Note Sample Code is simplified in reality this is used to populate a <a href="https://www.w3schools.com/tags/tag_datalist.asp" rel="noreferrer">datalist</a> via AJAX to Create an autocomplete experience. That object has 20+ properties and I can complete by writing 20+ methods but I feel there should be a DRY way to complete this. Also making this one method also would clean up my controller action a bunch also. </p>
  
  <p><strong>Question:</strong> </p>
  
  <p>Given the first section of code is there a way to abstract those similar methods into a single method buy passing some object into the select Statement???</p>
  
  <p>Thank you for your time.</p>