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 }
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.
What if you iterate the FastExpandoObject and return a filtered 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;
});
Related
I have the following code, where page.Fields is an ExpandoObject. I'm iterating through some user-defined properties and adding them to an Expando, cast to an IDictionary<string,string> to allow me to add new field name / values dynamically, but when I set the Fields property to the value of props, serializing afterwards only gives names with blank values of {}. Why?
page.Fields.Foo = "asdf";
var test = JsonConvert.SerializeObject(page); // shows Foo=asdf in the json
// attach all fields to the page object, casting to an IDictionary to be able to add var names
var props = new ExpandoObject() as IDictionary<string, Object>;
foreach (string key in Request.Form.Keys)
{
if (key.StartsWith("Fields."))
{
var fieldName = key.Substring(key.IndexOf(".") + 1);
props.Add(fieldName, Request.Form[key]);
}
}
var test2 = JsonConvert.SerializeObject(props); // blank values of {}
page.Fields = props as ExpandoObject;
// loses the values for the Fields property
test = JsonConvert.SerializeObject(page);
UPDATE the curse of Nancy strikes, the Request.Form values turned out to be dynamic, so I had to .ToString() it to make it fit into the expected IDictionary<string,string>
To correctly serialize the data you must declare the variable as dynamic, not as an ExpandoObject, JSON .net uses reflection to retrieve properties, if it's a dynamic it casts it as an ExpandoObject and uses the keys as property names, but if you pass the ExpandoObject directly it tries to retrieve the properties from the ExpandoObject type.
Just change
var props = new ExpandoObject() as IDictionary<string, Object>;
to
var props = new ExpandoObject();
var iProps = props as IDictionary<string, Object>;
Use iProps to add the data and pass props to the serialization.
EDIT:
You're storing the value in "Page.Fields", this must be dynamic also.
I'm suspect it's a defect and you are not getting anything for Request.Form.Keys that's matches Field. criteria.
your code works fine for me if I've a page class with dynamic Fields property
The Problem
Using C#, I need to traverse an object that has been cast to an ExpandoObject from XML and replace any "price" property with a new value.
This object is very unstructured and has many layers of nested nodes (nested ExpandoObjects, actually). More specifically, the hierarchy may look like this:
Product => price, quantity, accessories
Each accessory may have a price and quantity and may itself have accessories, this is why I need recursion.
What I have so far
public ExpandoObject UpdatePricing(ExpandoObject exp)
{
//Is there a price property?
var hasPrice = exp.Any(a => a.Key == "price");
if (hasPrice)
{
//update price here
exp.price = 0; //Some other price
}
//Now loop through the whole object. If any of the properties contain an expando, then call this method again
foreach (var kvp in exp)
{
if (kvp.Value is ExpandoObject)
{
//THIS CODE IS NO GOOD BECAUSE kvp.Value has no setter!!
kvp.Value = UpdatePricing(kvp.Value);
}
}
return exp;
}
The problem I run into is that the kvp.Value has no setter, so I can't run this method recursively.
Any suggestions are appreciated. Thanks!
Since ExpandoObject implements IDictionary<string, Object> things can get a bit easier. We can also change the return type to void because we don't need to reassign the result.
void UpdatePrice(ExpandoObject expando, decimal price)
{
var map = (IDictionary<string, Object>)expando;
if (map.ContainsKey("price"))
map["price"] = price;
foreach (var value in map.Values)
{
if (value is ExpandoObject)
UpdatePrice((ExpandoObject)value, price);
}
}
I don't know much about ExpandoObject. But like most dictionary implementations, I assume that in general if you want your key-value pair to be updated to have a different value, you need to go through the dictionary interface.
Note that you (probably) won't be allowed to modify the dictionary while you're enumerating its contents. So you'll need to build a list of elements to update and do that in a separate operation.
For example:
List<string> keysToUpdate = new List<string>();
foreach (var kvp in exp)
{
if (kvp.Value is ExpandoObject)
{
keysToUpdate.Add(kvp.Key);
}
}
foreach (string key in keysToUpdate)
{
exp[key] = UpdatePricing(exp[key]);
}
You could also keep the whole KeyValuePair value in your list, to avoid the second retrieval of the value, but I'm guessing that's not an important optimization here.
I just ran a little test on this and was able to get it to work by having the expando be dynamic:
public static ExpandoObject DoWork(ExpandoObject obj)
{
dynamic expando = obj;
//update price
if (obj.Any(c => c.Key == "price"))
expando.price = 354.11D;
foreach (var item in expando)
{
if (item.Value is ExpandoObject)
{
//call recursively
DoWork(item.Value);
}
}
return expando;
}
it elimitates type safety, but it looks like you don't have that luxury anyways, dynamic is the best way to interact with expandos in fact according to MSDN:
"In C#, to enable late binding for an instance of the ExpandoObject
class, you must use the dynamic keyword. For more information, see
Using Type dynamic (C# Programming Guide)."
this means that if you don't use the dynamic keyword, you are running the Expando in the CLR instead of the DLR which will have some odd consequences like not being able to set values. Hopefully this helps.
Is it possible select an anonymous type via list of properties as parameter. The method should look like:
public void TestLinq(List<"Properties"> properties, List<Data> data)
{
var dat = from d in data select new { properties };
}
I know the description sounds clumsy but I hope I get some help.
It would be important to know the term I have to look for this topic.
You can use the Dynamic LINQ query library (download the sample) to create the list of properties in your projection, like so:
public dynamic TestLinq(IEnumerable<Data> data, IEnumerable<string> properties)
{
// Validate parameters.
if (properties == null) throw new ArgumentNullException("properties");
if (data == null) throw new ArgumentNullException("data");
// Construct the field list.
var fields = new StringBuilder();
foreach (string p in properties) fields.AppendFormat("{0},", property);
// Throw an exception if there are no items.
if (fields.Length == 0) throw new ArgumentException(
"The properties enumeration contains no elements.", "properties");
// Remove the last comma.
fields.Length--;
// Select the items and return. Create the
// projection here.
return data.Select("new(" + fields + ")");
}
Note that the return type is of type dynamic, so you'll have no compile-time checking, and unless you're duck-typing, you probably won't have much knowledge of the fields.
You might be better off creating strong types for this, depending on your needs (if this is based on user-input, then you can't obviously).
Here you go, this is based on this answer https://stackoverflow.com/a/5310828/491950
List<string> properties = new List<string>() { {"ResultPrefix"}, {"ProfileResult"}};
foreach (dynamic d in ListProperties(properties, cellValues))
{
Console.WriteLine(d.ResultPrefix);
}
public static List<dynamic> ListProperties(List<string> properties, List<ChemistryResult> chemistryResults)
{
List<dynamic> output = new List<dynamic>();
foreach (ChemistryResult chemistryResult in chemistryResults)
{
IDictionary<string, Object> result = new ExpandoObject();
foreach (string property in properties)
{
PropertyInfo propertyInfo = typeof(ChemistryResult).GetProperty(property);
result[property] = propertyInfo.GetValue(chemistryResult);
}
output.Add(result);
}
return output;
}
You cannot use anonymous types in a method signature. It cannot be used as a parameter or the return type.
What you could do, is declare the parameter as dynamic, but dynamic can get really sticky, so I recommend avoiding it. You could have a List<dynamic> parameter, then you will be able to access members of the type, but you will not get type checking at compile time.
Another option it to use IEnumerable or IList. Using either of these will allow you to access the members of the collection without knowing the type. This is safer, as you have all of your compile time checks, but will not allow you to access members or the anonymous type.
But really, you should just convert your anonymous type into a real class so you can make your life easier.
I am sorry for the confusion. The outcome should be a csv that's right. The user should be able to define the order of the columns. But for me it was very difficult to formulate a good question. I am looking for a solution with expresisons not with reflection. My Idea was to generate a List of anonymous objects (with the right order) and out of them I wanted to create the csv. So I know the following is working:
public void Get(List<Value> data,Expression<Func<Value, T>> converter)
{
var dat = from d in data
select
new
{
converter
};
}
Is it possible to safe the Expression> converter in a property and combine many of them to one? So I would get the corret order
I understand why there isn't a method built in to do this, however I want a collection object that will allow Value change possibly during Enumeration.
Imagine the following:
Dictionary<string, List<string>> test = new Dictionary<string, List<string>> {{"key", null}};
Lets say I have 20 of these within a class which implements IEnumberable
I'd like to use lambda or a simple foreach to iterate through the class and find the object matching a key, then store my List<T> with the Value parameter.
You might be looking for a collection called multimap. See here for my implementation of it.
As you have discovered you can't change a DictionaryEntry through the Value property - you have to go through the Item accessor using the Key.
One option is to turn your Where results to an array then loop to get the matching Keys:
Dictionary<string, List<string>> test = new Dictionary<string, List<string>>
{{"key", null}};
test.Add("newKey",null);
var matches = test.Where(di => di.Key == "key").ToArray();
foreach(var di in matches) {
test[di.Key] = new List<string> {"one","two"};
You can just use this to modify a value in a dictionary:
public static void ChangeValue(string indexKey,List<string> newValue)
{
if (test.ContainsKey(indexKey))
keysDictionary[indexKey] = newValue;
}
You could avoid using an enumerator altogether, e.g.
var data = myEnumerable.ToArray();
for(var i = 0; i < data.Length; i++) {
// here you can manipulate data[i] to your heart's content
}
//mDIco is a Dictionnary with string as keys and homemade class (cAsso) as values
IEnumerator iterdico = mDico.GetEnumerator();
iterdico.Reset();
while (iterdico.MoveNext())
{
var asso = iterdico.Current as cAsso;
if (asso != null)
{
//Code
}
}
I thought this would work, but obviously it doesnt. So how I do i get access to the class which is contained into the value of my dictionnary?
The problem is that you are relying on the non-generic IEnumerator interface, which doesn't reveal the real element-type (its Current property is of type object). Use the generic interface (IEnumerator<T>, which does make the element-type easily discoverable) instead, and you will be fine.
Of course, you don't need any special effort for this. The Dictionary<,> class implements the IEnumerable interface explicitly. Its 'implicit' GetEnumerator method returns an enumerator that is strongly typed (a nested type that implements the generic interface), which is what we want.
So it's fine to use implicit typing all the way and let the compiler figure things out.
// Actually a Dictionary<string, cAsso>.Enumerator
// which in turn is an IEnumerator<KeyValuePair<string, cAsso>>
using(var iterdico = mDico.GetEnumerator())
{
while (iterdico.MoveNext())
{
// var = KeyValuePair<string, cAsso>
var kvp = iterdico.Current;
// var = string
var key = kvp.Key;
// var = cAsso
var value = kvp.Value;
...
}
}
EDIT:
A few other peripheral points:
In general, you should Dispose of enumerators, typically with a using block.
The use of the Reset method on enumerators is not recommended. In fact, in this particular case, it is useless.
Note that the element-type of the dictionary's enumerator is a Key-Value pair, not the value itself. if you are only interested in the values, enumerate the sequence returned by the dictionary's Value property.
As Davide Piras points out, in most cases, you just want a normal foreach loop instead of messing around with the enumerator yourself.
foreach(KeyValuePair<string, cAsso> kvp in mDico)
{
// kvp.Key is string
// kvp.Value is cAsso
}
foreach (var kvp in mDico)
{
var asso = kvp.Value;
...
}