title: C# What is the best practice to pass a parameter as method argument to both get AND set data from a generic class
body: <p>I'm fairly new to extension methods and i was wanting to make something very generic to re-order any type of list of an Entity Framework DbSet&lt;TEntity&gt;. To do so, i'd need to precise what property the entity is using as it's &quot;Order&quot; field. The most elegant way that was working for me was by using the property name (string) as the argument to then use it to get the PropertyInfo.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public static void OrderList&lt;TModel&gt;(this List&lt;TModel&gt; list, string propertyName)
  {
    PropertyInfo property = typeof(TModel).GetProperty(propertyName);
    list.OrderList(property);
  }
  
  public static void OrderList&lt;TModel&gt;(this List&lt;TModel&gt; list, PropertyInfo property)
  {
    var order = (int)list.Min(x =&gt; property.GetValue(x));
    list.ForEach(x =&gt; { property.SetValue(x, order++); });
  }
  
  // Called this way:
  myList.OrderList(nameof(Class.OrderProperty));
  
  </code></pre>
  <p>The thing is that i'm not much of a fan of the whole &quot;nameof(Class.Property)&quot;... Would there be a better way to do so? I tried to use <code>Func&lt;TModel, int&gt; property</code> because i REALLY like how you call these <code>list.function(x =&gt; x.Property)</code>, i really think it's the easiest one to understand. But I've come to learn that you can only get data from this, to set data i'd have to use an &quot;<em>Action</em>&quot; delegate to also set data, but then i'd have to enter the same property twice and it's not helping much... Only other option i can think of is using <code>System.Linq.Expressions</code> but i'm very unfamiliar with those and have no idea how to use them or even if it could solve my problem...</p>
  <p>Hope this made sense and thank you for taking the time to read this!</p>