In LINQPad, I frequently want to Dump some SQL table, but add a property to each row. I usually default to the simple:
Publishers
.Select(p => new { p, AppCount = p.Apps.Count() })
But of course this results in messy output, because each item is formatted as a vertical table, with an extra column containing the added property. I could create a new object, but that's very verbose, and you lose the table traversal links:
Publishers
.Select(p => new { p.PublisherId, p.Name, p.Added, ..., AppCount = p.Apps.Count() })
What I'd love is some sort of merge syntax, which creates a dynamic object to dump with the added property:
Publishers
.Select(p => p.Merge(new { AppCount = p.Apps.Count() })
I've tried a few different ways to accomplish that: using JsonConvert.SerializeObject (this goes haywire trying to serialize all the foreign key references and repeating loops, using ExpandoObject (couldn't come up with a nice clean syntax), and libraries like https://github.com/kfinley/TypeMerger, which I haven't gotten to work either.
Has anyone come up with a clean way to do this? Does LINQPad have some built in synax for this I can use? Thanks!
UPDATE: I uploaded a sample script to demonstrate the issue and let anyone try their solutions against: http://share.linqpad.net/kclxpl.linq
To run, you'll need to populate the Nutshell database, which you can do using the "Populate demo database" sample in LINQPad.
public int AppCount => Apps.Count()
? – Castleman