Starting with EF 4.1 this is possible. The only requirement is to know what the SP will return in each case.
In my example, I use
DbContext.Database.SqlQuery<TElement>(string sql, params object[] parameters)
This general method takes as a general parameter the type that you would like to use for materialization. It also accepts the SQL expression that you want to execute as a parameter, as well as parameters as param'd.
Simplified use of SP (without input parameters):
var res = ctx.Database.SqlQuery<MyResultType1>("dbo.MyStoredProcedure"); foreach (var r in res) { System.Console.Out.WriteLine( "col1:{0}; col2:{1}; col3={2}", r.Col1, r.Col2, r.Col3); }
So you can do the following:
IEnumerable res if(...your logic...) { res = ctx.Database.SqlQuery<MyResultType1>(...your SP call...); } else { res = ctx.Database.SqlQuery<MyResultType2>(...your SP call...); }
This way you are going to fill your collection with the help of the SP output results the way you want.
OzrenTkalcecKrznaric
source share