I am trying to pass a stored procedure mapped to a method called ListLeagueLeaders() but the autogenerated method to invoke a stored procedure, ObjectResult<ListLeagueLeaders_Result> ListLeagueLeaders() is expecting an ObjectResult type. What can I do to fix this?
I thought of using LINQ instead of a stored procedure, but I want to give this tactic a last chance. Related: LINQ to Entities instead of stored procedure?
HomeController
public class HomeController : Controller
{
HockeyStatsEntities db = new HockeyStatsEntities();
public ActionResult Index()
{
ViewBag.Message = "League leaders";
{
return View(db.ListLeagueLeaders());
}
}
private ICollection<ListLeagueLeaders_Result> ListLeagueLeaders()
{
ICollection<ListLeagueLeaders_Result> leagueLeadersCollection = null;
using (HockeyStatsEntities context = new HockeyStatsEntities())
{
foreach (ListLeagueLeaders_Result leagueLeader in
context.ListLeagueLeaders())
{
leagueLeadersCollection.Add(leagueLeader);
}
}
return leagueLeadersCollection;
}
}
Model1.Context.cs
public partial class HockeyStatsEntities : DbContext
{
public DbSet<Dim_Date> Dim_Date { get; set; }
public DbSet<Dim_Player> Dim_Player { get; set; }
public DbSet<Dim_Team> Dim_Team { get; set; }
public DbSet<Fact_Statistics> Fact_Statistics { get; set; }
public DbSet<Dim_Game> Dim_Game { get; set; }
public virtual ObjectResult<ListLeagueLeaders_Result> ListLeagueLeaders()
{
((IObjectContextAdapter)this).ObjectContext.MetadataWorkspace.LoadFromAssembly(typeof(ListLeagueLeaders_Result).Assembly);
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<ListLeagueLeaders_Result>("ListLeagueLeaders");
}
}
View:
@model NHLStats2.Models.ListLeagueLeaders_Result
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
You are getting this error on your View. You need to reference the same object type which you are retuning from your controller action.
For example, if you are returning type of
List<Foo>from your controller action, you can only make your view strongly typed toList<Foo>orIList<Foo>orIEnumerable<Foo>and etc.