title: sorting, filtering, and paging with entity framework core 2
body: <p>I'm creating a tabular grid of data and will have every column header sortable (both ascending and descending) as well as filterable by a textbox underneath the column name.</p>
  
  <p>I've done some digging to find out the best approach to generating my LINQ queries for entity framework and best I found was this: <a href="https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-3.0" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-3.0</a></p>
  
  <p>That just uses a big switch / case (for ordering only...doesn't even consider multiple filtering options beyond one).  Surely there's got to be a better, more concise approach than this?</p>
  
  <p>I'm half tempted to just use my old sproc and call the sproc with EF if this is really the path I'll have to go down.</p>
  
  <p>Ideally, I'd want something like this:</p>
  
  <pre><code>    return myContext.Where(x =&gt; 
  x.%thisisadynamicfield%.Contains("%somefiltervalue%" &amp;&amp; 
  x.%anotherdynamicfield%.Contains("%someothervalue%" &amp;&amp; ...)
  .OrderBy(x =&gt; x.%someorderybydynamicfield%)
  </code></pre>
  
  <p>I'm ok using a conditional to determine ordering ascending or descending, but I'd really like to compact everything else as much as possible and that MS article I linked above looks like code vomit and it would only cover part of my needs.</p>
  
  <p>EDIT: as is almost always the case, as soon as I post here and try one more searching effort, I find what I'm looking for.  I just found this: <a href="https://github.com/harshwPro/EntityFrameworkPaginate" rel="nofollow noreferrer">https://github.com/harshwPro/EntityFrameworkPaginate</a> which looks very promising.  I will report back here if that solves my issue.</p>
  
  <p>EDIT 2: well the github project I referenced in my first edit is certainly a cleaner solution than the MS article I posted originally but it still leaves me with writing a huge conditional to generate my filters by property.  Using that solution, I'd have to write something like this:
  <code>dataPagingModel.Filters.Add(true, x =&gt; x.MyCoolProperty.Contains("blah"));</code> but would have to conditionally add new filters based on my angular presentation layer's posted parameters - switch / case, or some huge if / else block.  What I'd ideally like is a way to just loop over all of the posted filters list and dynamically add those filters in a loop.  So I guess my question now boils down to, is there a way to access properties of an object dynamically (generically) in a lambda expression?</p>
  
  <p>EDIT 3: I think this SO post has the answers I need: <a href="https://stackoverflow.com/questions/20386634/generic-method-to-filter-a-list-object">Generic method to filter a list object</a></p>