无法隐式转换类型'System.Linq.IQueryable< Project.Models.TaskTimeLine>'到"Project.Models.TaskTimeLine" [英] 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.
这是我遇到的错误.
无法将类型'System.Linq.IQueryable'隐式转换为'Project.Models.TaskTimeLine'.存在显式转换(您是否缺少演员表?)
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.
这是类的定义
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
是TaskTimeLine
;为什么还会是IQueryable<TaskTimeLine>
?
TaskTimeLine taskTimeLine = new TaskTimeLine(); // create new main object
....
return (IQueryable<TaskTimeLine>)taskTimeLine;
IQueryable<T>
与数据的来源有关-与数据本身的实例无关(可能的例外:Enumerable.AsQueryable()
).
IQueryable<T>
relates to sources of data - not instances of the data itself (possible exception: Enumerable.AsQueryable()
).
我怀疑您的方法GetTaskAndTimeLine
应该只返回TaskTimeLine
;并丢掉了很多演员...
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);
这篇关于无法隐式转换类型'System.Linq.IQueryable< Project.Models.TaskTimeLine>'到"Project.Models.TaskTimeLine"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!