linq - C# - How do I get an anonymous type and then use the same selector to create an object? -
so i'm trying out azure's new documentdb. unfortunately doesn't allow selectors type. won't let me do:
public ienumerable<u> getall<u>(expression<func<t, u>> selector) { return client.createdocumentquery<t>(collection.documentslink) .select(selector) .asenumerable(); } and use as:
// doesn't work return getall(t => new myviewmodel { id = t.id, name = t.name, email = t.email, url = t.url }); it says supports anonymous types. (i imagine since it's new change @ point).
i can solve problem using second select:
return getall(t => new { id = t.id, name = t.name, email = t.email, url = t.url }).select(t => new myviewmodel() { id = t.id, name = t.name, email = t.email, url = t.url }).asenumerable(); however pain.
is there way use same selector twice , make anonymous first time?
you'll notice signature getall contains expression<func<t, u>> selector. expression part says argument being passed expression tree , not func delegate. it's done way provider can parse selector , generate appropriate database calls (often sql) fetch data.
now, when use specific custom type - in case myviewmodel - provider hits problem - doesn't know how convert type database calls. know how translate anonymous type though.
in returns ienumerable<u> data returned getall in memory can perform subsequent creation of myviewmodel type. there's no need translate database calls more.
so answer design. unlikely new feature in future.
Comments
Post a Comment