c# - Return Anonymous Type using SqlQuery RAW Query in Entity Framework -
how can make entity framework sqlquery return anonymous type.
right run context.theobject.sqlquery() raw query. query joins 2 tables , want return results of joined tables.
if use type context.theobject.sqlquery() see results of table of same type.
i tried db.database.sqlquery<dbresults>("the sql query here"); pre-defined class matches result's objects, fields null.
using entity framework 6 mysql.
i'm going out on limb here, , try address underlying problem instead of directly answering question.
your scenario pre-defined class should work. pitfall column names , properties of class did not match up.
sample code (linqpad)
var results = database.sqlquery<testresult>("select r.name, b.bankname relation r inner join bankaccount b on b.relationid = r.id r.id = 2"); results.dump(); } public class testresult { public string name { get; set; } public string bankname { get; set; } i'd advise revisit problematic code using explicit types.
in direct response question: no, can't return anonymous types sqlquery. best can build dynamic objects, unfortunately requires fair bit of manual work using typebuilder. see http://www.codeproject.com/articles/206416/use-dynamic-type-in-entity-framework-sqlquery sample.
Comments
Post a Comment