简体   繁体   English

System.NotSupportedException:'LINQ to Entities中不支持指定的类型成员'StartDateTime'

[英]System.NotSupportedException: 'The specified type member 'StartDateTime' is not supported in LINQ to Entities

After running my project visual studio has shown me this System.NotSupportedException in my Index.cshtml 运行我的项目后,Visual Studio在我的Index.cshtml中向我展示了此System.NotSupportedException

在此处输入图片说明

Here's my HomeController 这是我的HomeController

public class HomeController : BaseController
{
    public ActionResult Index()
    {
        var events = this.db.Events
            .OrderBy(e => e.StartDateTime)
            .Where(e => e.IsPublic)
            .Select(e => new EventViewModel()
            {
                Id = e.Id,
                Title = e.Title,
                Duration = e.Duration,
                Author= e.Author.FullName,
                Location = e.Location
            });

        var upcomingEvents = events.Where(e => e.StartDateTime > DateTime.Now);
        var passedEvents = events.Where(e => e.StartDateTime <= DateTime.Now);
        return View(new UpcomingPassedEventsViewModel()
        {
            UpcomingEvents = upcomingEvents,
            PassedEvents = passedEvents
        });
    }
}

} }

Here is my EventViewModel.cs 这是我的EventViewModel.cs

public class EventViewModel
{
    public int Id { get; set; }

    public string Title { get; set; }

    public DateTime StartDateTime { get; set; }

    public TimeSpan? Duration { get; set; }

    public string Author { get; set; }

    public string Location { get; set; }
}

You're projecting your entities into a view model. 您正在将实体投影到视图模型中。 That's a good thing to do, but afterwards you can't refine the query for your viewmodel again. 这样做是一件好事,但是之后您将无法再次优化视图模型的查询。 You're querying on EventViewModel.StartDateTime , which you don't even map. 您正在查询EventViewModel.StartDateTime ,甚至没有映射。

You need to add the query before mapping. 您需要在映射之前添加查询。 Of course you don't want to duplicate the mapping code, so put it in a method: 当然,您不想复制映射代码,因此将其放在方法中:

public ActionResult Index()
{ 
    var events = this.db.Events
                     .OrderBy(e => e.StartDateTime)
                     .Where(e => e.IsPublic);

    var upcomingEvents = events.Where(e => e.StartDateTime > DateTime.Now);
    var passedEvents = events.Where(e => e.StartDateTime <= DateTime.Now);

    return View(new UpcomingPassedEventsViewModel()
    {
        UpcomingEvents = upcomingEvents.Select(Map).ToList(),
        PassedEvents = passedEvents.Select(Map).ToList()
    });
}

private EventViewModel Map(EventDataModel e)
{
    return new EventViewModel()
    {
        Id = e.Id,
        Title = e.Title,
        Duration = e.Duration,
        Author= e.Author.FullName,
        Location = e.Location
    };
}

"BugFinder" answered my question. “ BugFinder”回答了我的问题。 My events doesn't hold StartDateTime. 我的事件没有保存StartDateTime。 So, here is the answer 所以,这就是答案

var events = this.db.Events
            .OrderBy(e => e.StartDateTime)
            .Where(e => e.IsPublic)
            .Select(e => new EventViewModel()
            {
                Id = e.Id,
                Title = e.Title,
                StartDateTime = e.StartDateTime,
                Duration = e.Duration,
                Author= e.Author.FullName,
                Location = e.Location
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.NotSupportedException: &#39;LINQ to Entities 不支持 LINQ 表达式节点类型&#39;Invoke&#39;。&#39; - System.NotSupportedException: 'The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.' System.NotSupportedException使用DateTime吗? 在Linq to实体 - System.NotSupportedException using DateTime? in Linq to Entities Linq to EF系统不支持的异常System.NotSupportedException - Linq to EF system not supported exception System.NotSupportedException LINQ to Entities 不支持指定的类型成员“用户名” - The specified type member 'UserName' is not supported in LINQ to Entities LINQ to Entities 不支持指定的类型成员“标题” - The specified type member 'Title' is not supported in LINQ to Entities LINQ to Entities 不支持指定类型成员“状态” - Specified type member"Status" not supported in LINQ to Entities System.NotSupportedException: '不支持指定的路径格式。' - System.NotSupportedException: 'The specified path format is not supported.' LINQ to Entities不支持指定的类型成员&#39;Product&#39; - The specified type member 'Product' is not supported in LINQ to Entities EF 4:LINQ to Entities不支持指定的类型成员'*' - EF 4: The specified type member '*' is not supported in LINQ to Entities LINQ to Entities不支持指定的类型成员 - The specified type member is not supported in LINQ to Entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM