I’m trying to get data from db.
my controller code:
var data = datacontext.executequery<dynamic>("select id, name from sometable");
return View(data);
my view code:
@model IEnumerable<dynamic>
@foreach(var item in Model)
{
@item.name
}
so when I run it I get error
‘object’ does not contain a definition for ‘name’
This has no business of working because when you are telling LINQ to SQL to do
datacontext.executequery<dynamic>you are effectively telling it to performdatacontext.executequery<object>Therefore everything your query returns is of, well,
objecttype.The reason for that, is that mapping of the returned types happens by inspecting with reflection the T passed at compile type, and since you passed dynamic, at compile time it’s equivalent to object.
If you really want to do something like that (dynamic API), you may want to look at Dapper – http://code.google.com/p/dapper-dot-net/