本文介绍了LINQ:无法转换类型'System.Int32'输入'System.Object的“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的MVC和EF和LINQ。

I'm new to MVC and EF and LINQ.

我用EF和DB首先创建所有从数据库模型。我还创建了一个视图模型CourseDetailsVM:

I used EF and DB first to create all the models from the DB. I also created a ViewModel "CourseDetailsVM":

public class CourseDetailsVM
{
    public int CourseId { get; set; }
    public int CategoryFk { get; set; }
    public int SoftwareFk { get; set; }
    public int TeacherFk { get; set; }
    public string CourseCode { get; set; }
    public string CourseCodeMs { get; set; }
    public string CourseName { get; set; }
    public string CourseDescriptionShort { get; set; }
    public int CourseEventId { get; set; }
    public int? CourseEventDurationInDays { get; set; }
    public int? CourseEventPrice { get; set; }
    public int CourseEventDateId { get; set; }
    public DateTime? CourseEventDateTimeFrom { get; set; }
    public DateTime? CourseEventDateTimeTo { get; set; }
}

在我的课程控制器我尝试使用摆脱视图模型模型下面的查询(在linqpad,但不是在MVC工作),我也使用的ViewData来访问存储库(这工作正常):

In my Course Controller I try to get the Model from the Viewmodel using the following query (which works in linqpad but not in mvc) and I also use ViewData to access the repository (this works fine):

// GET: /Course/Details/5

    public ActionResult Details(int id)
    {
        // get courses to display course details using ViewData
        ViewData["Courses"] = _repository.GetCourses(id);

        // get course events
        var result = (from c in db.Courses
                      join ce in db.CourseEvents
                      on c.CourseId equals ce.CourseFk
                      join ced in db.CourseEventDates
                      on ce.CourseEventId equals ced.CourseEventFk
                      group ce by new { ce.CourseEventId, c.CourseId, c.CourseCode, c.CourseName, ce.CourseEventPrice } into grp
                      select new CourseDetailsVM
                      {
                          CourseEventId = grp.Key.CourseEventId,
                          CourseId = grp.Key.CourseId,
                          CourseCode = grp.Key.CourseCode,
                          CourseEventDateTimeFrom = (from c2 in db.CourseEventDates where (c2.CourseEventFk.Equals(grp.Key.CourseEventId)) select c2.CourseEventDateTimeFrom).Min(),
                          CourseEventDateTimeTo = (from c2 in db.CourseEventDates where (c2.CourseEventFk.Equals(grp.Key.CourseEventId)) select c2.CourseEventDateTimeFrom).Max(),
                          CourseEventDurationInDays = (from c2 in db.CourseEventDates where (c2.CourseEventFk.Equals(grp.Key.CourseEventId)) select c2.CourseEventDateTimeFrom).Count() / 2,
                          CourseName = grp.Key.CourseName,
                          CourseEventPrice = grp.Key.CourseEventPrice,
                          CourseEventDateId = grp.Key.CourseEventId
                      }).OrderBy(c => c.CourseEventDateTimeFrom);

    return View("Details", result);
    }



由于LINQ的运行上编译,我可以编译该项目没有任何错误。但访问课程时/详细查看我得到以下错误:
无法投类型'System.Int32'输入'System.Object的。 LINQ到实体仅支持铸造EDM基元或枚举类型

在以下领域作出CourseDetailsVM的一个新的实例时出现的错误: CourseEventDateTimeFrom,CourseEventDateTimeTo,CourseEventDurationInDays

The error occurs when making an new instance of CourseDetailsVM on the following fields: CourseEventDateTimeFrom, CourseEventDateTimeTo, CourseEventDurationInDays

我试图填补使用硬编码值所提到的属性和它工作得很好。所以我知道,那里的错误发生,但我不知道为什么,以及如何解决它。

I tried fill the mentioned properties using hard coded values and it worked fine. So I know, where the error occurs, but I don't know why and how to solve it.

由于所要求的Yuliam钱德拉的CourseEventDate类的内容(由EF生成):

As requested by Yuliam Chandra, the content of the CourseEventDate class (generated by EF):

public partial class CourseEventDate
{
    public int CourseEventDateId { get; set; }
    public Nullable<int> CourseEventFk { get; set; }
    public Nullable<System.DateTime> CourseEventDateTimeFrom { get; set; }
    public Nullable<System.DateTime> CourseEventDateTimeTo { get; set; }
    public virtual CourseEvent CourseEvent { get; set; }
}



我希望某人你们能帮助我。

I hope that sb of you guys can help me.

感谢您,

罗宁

推荐答案

比较时可能造成的误差 CourseEventFk INT?)和 CourseEventId INT )使用等于,其中的参数接受的对象类型,它是一个CLR方法。 LINQ到实体不支持CLR的方法。

The error could be caused when comparing CourseEventFk (int?) and CourseEventId (int) using Equals where its parameter accepts object type which is a CLR method. LINQ to Entities doesn't support CLR method.

尝试使用 == 运营商,这多少有点L2S兼容,在下面的代码。

Try to use == operator, which somewhat compatible to L2S, on following code.

CourseEventDateTimeFrom = ..where (c2.CourseEventFk == grp.Key.CourseEventId)..
CourseEventDateTimeTo = ..where (c2.CourseEventFk == grp.Key.CourseEventId)..
CourseEventDurationInDays = ..where (c2.CourseEventFk == grp.Key.CourseEventId)..

这篇关于LINQ:无法转换类型'System.Int32'输入'System.Object的“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:05