Search code examples
c#linqdapper

Project dynamic Dapper results excluding some properties


Given a dynamic Dapper query such as:

var results = connection.Query<dynamic>("SELECT ....");

I want to remove a couple of the returned columns/properties from the results. The trick is I want to do this without knowing/caring the names of the properties I want to keep, otherwise I would simply project the results into a new anonymous type.

I attempted to loop through the results and cast each to an IDictionary<string, object> so that I could simply remove the key/value pair holding the data. Unfortunately, for whatever reason, the underlying internal FastExpando object doesn't implement the Remove method. The source code for FastExpando shows:

bool IDictionary<string, object>.Remove(string key)
{
    throw new NotImplementedException();
}

How can I implement this? To be clear I basically want:

var filteredResults = from r in results
                      select new { // All properties except a couple of well-known ones }

Solution

  • There was no reason that FastExpandoObject didn't implement those methods, other than: it hadn't needed to. I have now filled in all the missing / NotImplementedException methods. I haven't yet deployed to NuGet, but the code on github/google-code has been updated.