c# - Code First EF6 - How to translate to a custom object thats not an entity -


i have stored proc that's returning 2 columns. first id column entity (userid) next count of how many userids(count).

in code, translate dictionary, user ef entity.

var result = ((iobjectcontextadapter)dbcontext).objectcontext.translate<dictionary<user, int>>(reader).firstordefault(); 

this returns empty dictionary , appears translate isn't working way i'm hoping would.

i'm guessing since i'm returning userid it's not able materialize complete user entity.

can provide example of how end result i'm looking for?

look @ documentation:

the supplied dbdatareader must contain data maps requested entity type.

so there 2 things wrong in code:

  • the type you're trying materialize dictionary, not entity type.
  • and if entity type, reader wouldn't contain data maps requested entity type, i.e. rows values correspond entity's properties.

you have users separate query, using id values returned stored procedure. along these lines:

var sprocvalues = getstoredprocedurevalues(); // let's assume returns list of tuple<int,int>  var usrids = sprocvalues.select(t => t.item1).toarray(); var users = dbcontext.users.where(u => usrids.contains(u.userid)).toarray(); var result = u in users              join t in sprocvalues on u.userid equals t.item1              select new { user = u, count = t.item2 }; 

Comments