Cannot implicitly convert type 'System.Linq.IQueryable<Project.Models.TaskTimeLine>' to 'Project.Models.TaskTimeLine'
I'm trying to pass a custom shaped class to a view, but I'm having a little trouble.
This is the error I'm getting.
Cannot implicitly convert type 'System.Linq.IQueryable' to 'Project.Models.TaskTimeLine'. An explicit conversion exists (are you missing a cast?)
This is the first time I've added a list to a class, and trying to pass one inside the other as it were.
Here is the class definition
public class TaskTimeLine
{
public Task task { get; set; } // to hold a single object
public List<DateTime> scheduledDateTime { get; set; } // to hold a collection of date times..
}
The controller action is along the lines of this.
[Authorize]
public ActionResult Details2(int id)
{
TaskTimeLine task = new TaskTimeLine(); //create new instance of TaskTimeLine
task = (IQueryable<TaskTimeLine>)taskRepository.GetTaskAndTimeLine(id);
if (task == null)
return View("NotFound");
else
return View("Details", task);
}
All quite normal up until now, so I'm guessing maybe something is going wrong here.
public IQueryable<TaskTimeLine> GetTaskAndTimeLine(int taskId)
{
TaskTimeLine taskTimeLine = new TaskTimeLine(); // create new main object
taskTimeLine.scheduledDateTime = new List<DateTime>(); instanciate list object inside
taskTimeLine.task = db.Tasks.SingleOrDefault(d => d.id == taskId); read the record to fill the single task object
/// Lots of logic is performed here to add loads of date records to the list object.
/// If I add a break point and check the list I can see that all the date items have gone fine into the collection
return (IQueryable<TaskTimeLine>)taskTimeLine;
}
The only reason I have so many odd casts in places is because it seemed that these helped the program compile. It currently doesn't compile, but I did get开发者_运维问答 to, but when running the actual request it fails, with the same error message.
Can anyone help, also perhaps with add a more useful title for future people who are stuck with this kind of thing.
taskTimeLine
is a TaskTimeLine
; why would it also be an IQueryable<TaskTimeLine>
?
TaskTimeLine taskTimeLine = new TaskTimeLine(); // create new main object
....
return (IQueryable<TaskTimeLine>)taskTimeLine;
IQueryable<T>
relates to sources of data - not instances of the data itself (possible exception: Enumerable.AsQueryable()
).
I suspect your method GetTaskAndTimeLine
should simply return TaskTimeLine
; and lose a lot of the casts...
public TaskTimeLine GetTaskAndTimeLine(int taskId)
{
TaskTimeLine taskTimeLine = new TaskTimeLine(); // create new main object
...[snip]
return taskTimeLine;
}
...
TaskTimeLine task = taskRepository.GetTaskAndTimeLine(id);
I had this error too, and I solved it by adding FirstOrDefault()
to the line.
In this case, at this line:
task = (IQueryable<TaskTimeLine>)taskRepository.GetTaskAndTimeLine(id).FirstOrDefault();
(This is because IQueryable
expects to several object, and you cannot get some objects into one task
, only if you tell it to get only one by adding FirstOrDefault()
.)
In the Details2 method:
TaskTimeLine task = new TaskTimeLine(); //create new instance of TaskTimeLine
task = (IQueryable<TaskTimeLine>)taskRepository.GetTaskAndTimeLine(id);
Here you are declaring task to be a TaskTimeLine, then trying to load an IQueryable<TaskTimeLine>
into that variable. These types are not compatible.
Change your repository method to return a TaskTimeLine rather than an IQueryable (that will also get rid of the casting problem inside the GetTaskAndTimeLine method that Marc identifies), and remove the cast:
TaskTimeLine task = taskRepository.GetTaskAndTimeLine(id);
精彩评论