【发布时间】:2019-05-24 09:06:40
【问题描述】:
我想从数据库中选择一个日期时间格式的数据。
我有 3 个模型。
School
public int Id { get; set; }
public ICollection<SchoolStore> SchoolStores { get; set; }
SchoolStores
public int SchoolId { get; set; }
public School School { get; set; }
public ICollection<Event> Events { get; set; }
Events
public DateTime Date { get; set; }
public EventStatus Status { get; set; }
方法看起来像
public IQueryable<SchoolProjectionModel> GetAllForList()
{
return dataContext.Schools.AsNoTracking().IgnoreQueryFilters()
.Select(s => new SchoolListProjectionModel()
{
publishedEventDate = s.SchoolStores.Select(ss => ss.Events
.Where(ee => ee.Status == EventStatus.Published && ee.SchoolId == s.Id)
.Select(ed => ed.Date)
.Where(ses => ses.Date >= DateTime.Today))
});
}
但我收到这样的错误
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.DateTime>'
to 'System.DateTime'
我希望从数据库中接收日期时间格式的数据。
SchoolListProjectionModel
using System.Collections.Generic;
using System;
public class SchoolListProjectionModel
{
public DateTime publishedEventDate { get; set; }
}
【问题讨论】:
-
请告诉我们
SchoolListProjectionModel -
用模型更新了我的问题
-
在最后一个
.where之后添加.First()或.FirstOrDefault() -
只需将 .Where(condition) 替换为 .FirstOrDefault(condition) 即可获得单个日期而不是日期列表
-
我尝试添加和替换,但没有任何改变
标签: asp.net entity-framework linq asp.net-core