Dump only part of an Object
Hello together;
Does anybody know how to dump only a part of an object ?
I have Objects to dump but i dont want to see a certain column.
All objects to Dump differ from each other but all have one column with the same name i dont want to display.
Can someone help me with it?
thanks in advance
Klaus
Does anybody know how to dump only a part of an object ?
I have Objects to dump but i dont want to see a certain column.
All objects to Dump differ from each other but all have one column with the same name i dont want to display.
Can someone help me with it?
thanks in advance
Klaus
Comments
I am dumping to richtext (html).
Unfortunately the type is not my own custom type,
therefore your idea does'nt fit for my problem.
But anyway, thanks a lot.
klaus
Instead of hiding the entire column, you could wipe the contents of each row by setting the value to null (if applicable) in a loop:
.Where(t=>t.Something)
.Dump(t=>t.Count(),"Count after first Where")
.Where(t=>t.SomeOtherThing)
.Dump(t=>t.Contains(MyObject),"MyObject still present after second Where")
.Where(t=>t.OhAnotherThing)
Can of course be done with helper methods.
The Dump extension method now has an optional parameter for specifying a comma-delimited list of columns/members to exclude from the output. For example:
Customers.Dump (exclude:"ID, Purchases");
This feature works in both rich-text and datagrid mode.
http://www.linqpad.net/download.aspx#beta
Thanks a lot for fulfilling me wish.
That helps me very much
Have a good time;
klaus
Dump (xyz, format: "-ID, -Name") // exclude id and name
or
Dump (xyz, format:"+ID, +Name") // include id and name
It might be more confusing and less discoverable, though.
Dump (xyz, format:"ID:999")
Would that include *only* the ID column, or merely specify the desired format string for that column?
Here are some other ideas that might work
string columns = Util.DumpColumns(Persons,exclude:"Id,Name");
Persons.Dump(exclude:columns);
DumpSettings settings = new DumpSettings{
Include="Id,Name",
Format="Id:999"
};
Persons.Dump(settings);
Persons.Dump(x=>new{x.Id,x.Name});
Persons.Dump(exclude:"*,-Id,-Name");
Is there a way to get it to exclude recursively?
I am regularly dumping AST trees which can get very large and containing many irrelevant properties.
For example, excluding all location/position properties.
Could a possible approach be to create a new interface like ICustomMemberProvider, but with a property that is ultimately set by an exclude or similar parameter inside .Dump()? This way the string can be processed by the user as he/she desires.
For instance: For a more dynamic approach, create an ExpandoObject: The following dumps all properties except for those of type "XYZ":
As for my request to be able to pass parameters inside Dump() to ICustomMemberProvider, this would convert to a request to have ToDump take parameters passed via Dump(). But I see now that this isn't necessary, as I can just create fields or properties inside a class and reference them from ToDump to change the output behavior.
Thank you!
seems that the built-in .Dump(exclude:"propName") doesn't care about properties on the things inside an IEnumerable at all.
private static Dictionary<string, object> ToDictionary(object item) { var eitem = new Dictionary<string, object>(); foreach (var prop in item.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (prop.GetIndexParameters().Length == 0) try { eitem.Add(prop.Name, prop.GetValue(item)); } catch (Exception ex) { eitem.Add(prop.Name, ex); } } return eitem; }
public static ExpandoObject ToExpando(this IDictionary<string, object> d) { var e = new ExpandoObject(); var di = (IDictionary<string, object>)e; foreach (var kvp in d) di.Add(kvp); return e; }
public static IDictionary<TKey, TValue> Remove<TKey, TValue>(this IDictionary<TKey, TValue> d, TKey[] except) { foreach (var e in except) d.Remove(e); return d; }
public static IEnumerable<ExpandoObject> ToEnumExpando<T>(this IEnumerable<T> obj, string[] except = null) { return obj.Select(item => ToDictionary(item).Remove(except).ToExpando()); }
public static IEnumerable<T> Dump<T>(this IEnumerable<T> obj, string[] except, string descr = null) { var enumitems = obj.ToEnumExpando(except); if (descr != null) enumitems.Dump(descr); else enumitems.Dump(); return obj; }
It takes a string[] of fields to exclude to avoid conflicting with existing Dump() definitions. I have another version that creates an anonymous object instead of using the Expando, but it has a bit more code.
PS Sorry about the code formatting, couldn't figure out what the block expected.