Clued in after reading this...

I've got a place where I want to return entities mixed in with folders in a grid, in this case a Kendo Grid. I want to have the two "types" of data mixed in display, however, which means this isn't as clean as it could be. I've got pretty typical fields for the first query, and then I'm kinda kludging the folders into the same object model, like so...

using (AssetRepository repo = new AssetRepository(AccessControlHelper.GetCurrentUserId()))
    var assets = repo.GetAllAssets().Select(a => new {
        id = "a" + a.AssetId,
        clientId = a.ClientId,
        name = a.AssetName,
        tags = a.Tags,
        folderId = a.AssetFolderId,
        thumbnail = "/AssetManager/ImageThumbnail/" + a.AssetId

    var folders = repo.GetAssetFoldersForParentFolderId(null).Select(f => new
        id = "f" + f.AssetFolderId,
        clientId = f.ClientId,
        name = f.FolderName,
        tags = "",
        folderId = f.ParentFolderId,
        thumbnail = ""
    var both = assets.Union(folders);   // <<< NEAT-O, DADDY-O!

    return Json(assets);

I'm not absolutely sure how I feel about this as a production-worthy strategy... It's obviously taking a dog and making it quack like a duck, which is tres hipster. The problem is that Kendo Grid allows you to have hierarchies, but expects everything on the same hierarchical level to be of the same object type. (My vote is to write a custom table renderer that'd support the concept of folder levels mixed in with entities, but that's understandably not the route this contract is taking. Still, it's so often just as difficult to get a third-party library to work as you need it as it'd be to make your own widget that exactly covers your own use cases.)

Regardless, what's neat to me is that the compiler's smart enough to know these are the same anonymous types and allows you to Union them. That's cool.

(apologies for the Yoda'd blog title. SEO BABY! /sarcasmTinge)

Labels: , , ,