[英]linq datetime not contain max or min
I am getting this error that the datetime does not contain max or min method.我收到这个错误,日期时间不包含 max 或 min 方法。 I am trying to get the meeting max and min date and see if they are equal and then set MeetingDate to the min datetime.
我正在尝试获取会议的最大和最小日期,看看它们是否相等,然后将 MeetingDate 设置为最小日期时间。 If they are not equal then I will concat both min StartDate and max StartDate together.
如果它们不相等,那么我会将 min StartDate 和 max StartDate 连接在一起。 below is what the output should look like and the error I am getting is datetime not contain max or min method.
下面是 output 应该是什么样子,我得到的错误是 datetime 不包含 max 或 min 方法。
class Program
{
static void Main(string[] args)
{
List<MeetingModel> Meeting = new List<MeetingModel>();
Meeting.Add(new MeetingModel() { MeetingId = 1, startTime = new DateTime(2017, 1, 18) });
Meeting.Add(new MeetingModel() { MeetingId = 1, startTime = new DateTime(2017, 1, 19) });
Meeting.Add(new MeetingModel() { MeetingId = 2, startTime = new DateTime(2017, 1, 22) });
Meeting.Add(new MeetingModel() { MeetingId = 2, startTime = new DateTime(2017, 1, 22) });
Meeting.Add(new MeetingModel() { MeetingId = 3, startTime = new DateTime(2017, 1, 23) });
Meeting.Add(new MeetingModel() { MeetingId = 3, startTime = new DateTime(2017, 1, 23) });
Meeting.Add(new MeetingModel() { MeetingId = 4, startTime = new DateTime(2017, 1, 25) });
Meeting.Add(new MeetingModel() { MeetingId = 4, startTime = new DateTime(2017, 1, 29) });
var result = Meeting
.Select (p=> new MeetingViewModel
{
MeetingId = p.MeetingId,
MeetingDate = p.startTime.Max() == p.startTime.Min() ? p.startTime.Min().ToString("MM/dd/yyyy") :
string.Format("{0: MM/dd/yyyy} - {1: MM/dd/yyyy}", p.startTime.Min(), p.startTime.Max()
}).ToList();
foreach (var meeting in result)
{
Console.WriteLine("{0}", meeting.MeetingId);
Console.WriteLine("{0}", meeting.MeetingDate);
}
}
class MeetingModel
{
public int MeetingId { get; set; }
public DateTime startTime { get; set; }
}
class MeetingViewModel
{
public int MeetingId { get; set; }
public string MeetingDate { get; set; }
}
}
Error错误
Severity Code Description Project File Line Suppression State Suppression State Error CS1929 'DateTime' does not contain a definition for 'Max' and the best extension method overload 'Enumerable.Max(IEnumerable)' requires a receiver of type 'IEnumerable'严重性代码 描述 项目文件行抑制 State 抑制 State 错误 CS1929 'DateTime' 不包含 'Max' 的定义和最佳扩展方法 'Enumerable.Max(IEnumerable')' 需要接收器类型
Output Output
Meeting 1 2017/1/18 - 2017/1/19
Meeting 2 2017/1/22
Meeting 3 2017/1/23
Meeting 4 2017/1/25 - 2017/1/29
EF英孚
select new Model.Meeting
{
MeetingId = m.MeetingId,
GroupId = m.GroupId,
MeetingDate = m.Min(x => x.startTime) == m.Max(x => x.startTime) ?
m.Max(x => x.startTime).ToString("MM/dd/yyyy") :
string.Format("{0: MM/dd/yyyy} - {1: MM/dd/yyyy}",
md.Min(x => x.startTime), md.Max(x => x.startTime))
}).GroupBy(x => x.MeetingId).ToList();
You could group by MeetingId
and use .Min
and .Max
linq methods to fetch dates:您可以按
MeetingId
分组并使用.Min
和.Max
方法来获取日期:
var result = Meeting
.GroupBy(x => x.MeetingId)
.Select(p => new MeetingViewModel
{
MeetingId = p.Key,
MeetingDate = p.Min(x => x.startTime) == p.Max(x => x.startTime) ?
p.Max(x => x.startTime).ToString("MM/dd/yyyy") :
string.Format("{0: MM/dd/yyyy} - {1: MM/dd/yyyy}", p.Min(x => x.startTime), p.Max(x => x.startTime))
}).ToList();
Test in dotnetfiddle: https://dotnetfiddle.net/IJaehf在 dotnetfiddle 中测试: https://dotnetfiddle.net/IJaehf
I hope you find this helpful.我希望你觉得这有帮助。
Please try DateTime.MinValue
or SqlDateTime.MinValue
.请尝试
DateTime.MinValue
或SqlDateTime.MinValue
。 These are implemented as static readonly
fields and not methods.这些实现为
static readonly
字段而不是方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.