This I suppose wouldn't work, because EF
is not returning properties in the dynamic object at all, for explained case.
It works other way around, First EF
Takes all properties of provided Generic Type
attribute, Then it tries to map properties to SQL request results.
So if you don't know what is the list of fields will be as result EF
wouldn't generate it for you.
First solution will be to parse select list to define the fields and generate class dynamicaly. Check how to create dynamic class discussion. Than you can do something like
var dType = MyTypeBuilder.CompileResultType("sample", new Dictionary<string, Type>
{
{ "Id", typeof(int) }
});
var db = new Model1().Database;
var sql = db.GetType().GetMethods().Where(m => m.Name == "SqlQuery" && m.IsGenericMethod);
var gen = sql.First().MakeGenericMethod(dType);
var result = gen.Invoke(db, new object[] { "[Your SQL]", new object[0] });
var ie = (result as IEnumerable<object>);
that is really complex, but works
Second - you can do classic ADO SQLCommand
using context.Database.Connection
and parse results
Third way to solve the problem is to Return XML from SQL, if possible.
var query = db.SqlQuery<string>("[Your SQL] for XML PATH('item'), root('root')").First();
that will return all data in XML format Like:
<root>
<item>
<Id>5</Id>
...
</item>
<item>
<Id>6</Id>
...
</item>
</root>