[英]Project dynamic Dapper results excluding some properties
Given a dynamic Dapper query such as:给定一个动态的 Dapper 查询,例如:
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.我尝试遍历结果并将每个结果转换为
IDictionary<string, object>
以便我可以简单地删除保存数据的键/值对。 Unfortunately, for whatever reason, the underlying internal FastExpando object doesn't implement the Remove method.不幸的是,无论出于何种原因,底层的内部 FastExpando 对象都没有实现 Remove 方法。 The source code for FastExpando shows:
FastExpando 的源代码显示:
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 }
What if you iterate the FastExpandoObject
and return a filtered ExpandoObject
?如果您迭代
FastExpandoObject
并返回过滤后的ExpandoObject
怎样?
var filteredResults = dynamicResult.Select<dynamic, dynamic>(x =>
{
var filteredRow = new ExpandoObject() as IDictionary<string, Object>;
var props = x as IDictionary<string, object>;
foreach (var prop in props)
{
if (!filter.Contains(prop.Key))
filteredRow.Add(prop);
}
return filteredRow;
});
There was no reason that FastExpandoObject
didn't implement those methods, other than: it hadn't needed to. FastExpandoObject
没有实现这些方法,除了:它不需要。 I have now filled in all the missing / NotImplementedException
methods.我现在已经填写了所有缺失的/
NotImplementedException
方法。 I haven't yet deployed to NuGet, but the code on github/google-code has been updated.我还没有部署到NuGet,但是github/google-code上的代码已经更新了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.