This is my method that is giving the error.
public List<StatusViewModel> GetStatuses(){
using(var ctx = new AppStatusEntities()){
var result = ctx.GetLatestStatuses().Select(r => new StatusViewModel
{
r.ApplicationId,
r.ApplicationName,
r.ApplicationStatus,
r.LastRun
}).ToList();
return result;
}
}
StatusViewModel is a POCO.
public class StatusViewModel{
public Guid ApplicationId {get;set;}
public string ApplicationName {get;set;}
public string ApplicationStatus {get;set;}
public DateTime LastRun {get;set;}
}
The error message is
Cannot initialize type 'StatusViewModel' with a collection initializer because it does not implement 'System.Collections.IEnumerable'
I can only assume it has something to do with the return type of the stored procedure call being a “complex” as specified when I imported the function in the Entity Data Model. But I cannot figure out why that would matter. If I do something like ctx.ApplicationStatus.Select(r => new StatusViewModel {... where ApplicationStatus is a table and not a stored procedure call, then that code will compile without errors.
Try it this way:
The difference between your stored procedure call and direct access to ObjectSet is Linq implementation. Your first example with stored procedure uses projection your application using Linq-to-Objects whereas the second example uses projection in SQL using Linq-to-entities.