C# 从DateTime列中为每天选择最小时间值
有一个表列RecordDateTime:C# 从DateTime列中为每天选择最小时间值,c#,linq,entity-framework-core,C#,Linq,Entity Framework Core,有一个表列RecordDateTime: 20.2.2021 12:54:34 20.2.2021 13:54:34 20.2.2021 14:54:34 20.2.2021 15:54:34 20.2.2021 16:54:34 21.2.2021 11:50:00 21.2.2021 13:54:34 21.2.2021 14:54:34 22.2.2021 10:00:00 22.2.2021 13:54:34 22.2.2021 14:54:34 我需要接电话 2
20.2.2021 12:54:34
20.2.2021 13:54:34
20.2.2021 14:54:34
20.2.2021 15:54:34
20.2.2021 16:54:34
21.2.2021 11:50:00
21.2.2021 13:54:34
21.2.2021 14:54:34
22.2.2021 10:00:00
22.2.2021 13:54:34
22.2.2021 14:54:34
我需要接电话
20.2.2021 12:54:34
21.2.2021 11:50:00
22.2.2021 10:00:00
并计算平均时间
这是我目前的代码:
public TimeSpan BeginTime(DateTime startDate, DateTime endDate)
{
var mainGridInfo = from w in db.LogModel
where w.RecordDateTime.Date >= startDate.Date && w.RecordDateTime.Date <= endDate.Date
group w by w.RecordDateTime.Date into test
select test.OrderByDescending(cs => cs.RecordDateTime.TimeOfDay).FirstOrDefault();
var minDatesList = mainGridInfo.Select(s => s.RecordDateTime.TimeOfDay).ToList();
double doubleAverageTicks = minDatesList.Average(timeSpan => timeSpan.Ticks);
long longAverageTicks = Convert.ToInt64(doubleAverageTicks);
return new TimeSpan(longAverageTicks);
}
转换,或通过插入
调用“AsEnumerable”、“AsAsAsAsyncEnumerable”、“ToList”或
“ToListSync”。寻找
更多信息。”
请尝试以下操作:
DataTable dt = new DataTable();
dt.Columns.Add("Date", typeof(DateTime));
dt.Rows.Add(new object[] {DateTime.ParseExact("20.2.2021 12:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("20.2.2021 13:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("20.2.2021 14:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("20.2.2021 15:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("20.2.2021 16:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("21.2.2021 11:50:00", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("21.2.2021 13:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("21.2.2021 14:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("22.2.2021 10:00:00", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("22.2.2021 13:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] { DateTime.ParseExact("22.2.2021 14:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
var days = dt.AsEnumerable().GroupBy(x => x.Field<DateTime>("Date").Date).ToList();
DateTime[] minDate = days.Select(x => x.Min(y => y.Field<DateTime>("Date"))).ToArray();
请尝试以下操作:
DataTable dt = new DataTable();
dt.Columns.Add("Date", typeof(DateTime));
dt.Rows.Add(new object[] {DateTime.ParseExact("20.2.2021 12:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("20.2.2021 13:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("20.2.2021 14:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("20.2.2021 15:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("20.2.2021 16:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("21.2.2021 11:50:00", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("21.2.2021 13:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("21.2.2021 14:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("22.2.2021 10:00:00", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] {DateTime.ParseExact("22.2.2021 13:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
dt.Rows.Add(new object[] { DateTime.ParseExact("22.2.2021 14:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)});
var days = dt.AsEnumerable().GroupBy(x => x.Field<DateTime>("Date").Date).ToList();
DateTime[] minDate = days.Select(x => x.Min(y => y.Field<DateTime>("Date"))).ToArray();
假设我们有一些列表日期,我们应该按日期对它们进行分组,并从每个组中提取最小值,如下所示:
var groupedDates = dates.GroupBy(d => d.Date)
.Select(
d => d.OrderByAscending(x => x.Millisecond).FirstOrDefault());
假设我们有一些列表日期,我们应该按日期对它们进行分组,并从每个组中提取最小值,如下所示:
var groupedDates = dates.GroupBy(d => d.Date)
.Select(
d => d.OrderByAscending(x => x.Millisecond).FirstOrDefault());
再看看我的变体:
using System;
using System.Collections.Generic;
using System.Linq;
namespace Game
{
class LogModel
{
public DateTime RecordDateTime { get; set; }
}
class Program
{
static List<LogModel> _log = new List<LogModel>
{
new LogModel { RecordDateTime = DateTime.ParseExact("20.2.2021 12:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("20.2.2021 13:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("20.2.2021 14:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("20.2.2021 15:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("20.2.2021 16:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("21.2.2021 11:50:00", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("21.2.2021 13:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("21.2.2021 14:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("22.2.2021 10:00:00", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("22.2.2021 13:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("22.2.2021 14:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
};
static void Main(string[] args)
{
var result = _log.GroupBy(x => x.RecordDateTime.Date)
.Select(x =>
{
var minTime = x.Min(y => y.RecordDateTime.TimeOfDay);
return new DateTime(x.Key.Year, x.Key.Month, x.Key.Day, minTime.Hours, minTime.Minutes, minTime.Seconds);
})
.ToList();
result.ForEach(x => Console.WriteLine(x));
}
}
}
再看看我的变体:
using System;
using System.Collections.Generic;
using System.Linq;
namespace Game
{
class LogModel
{
public DateTime RecordDateTime { get; set; }
}
class Program
{
static List<LogModel> _log = new List<LogModel>
{
new LogModel { RecordDateTime = DateTime.ParseExact("20.2.2021 12:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("20.2.2021 13:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("20.2.2021 14:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("20.2.2021 15:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("20.2.2021 16:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("21.2.2021 11:50:00", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("21.2.2021 13:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("21.2.2021 14:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("22.2.2021 10:00:00", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("22.2.2021 13:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
new LogModel { RecordDateTime = DateTime.ParseExact("22.2.2021 14:54:34", "dd.M.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) },
};
static void Main(string[] args)
{
var result = _log.GroupBy(x => x.RecordDateTime.Date)
.Select(x =>
{
var minTime = x.Min(y => y.RecordDateTime.TimeOfDay);
return new DateTime(x.Key.Year, x.Key.Month, x.Key.Day, minTime.Hours, minTime.Minutes, minTime.Seconds);
})
.ToList();
result.ForEach(x => Console.WriteLine(x));
}
}
}
什么是您的DBMS,看起来像SQLite?您使用的是哪种Linq提供程序?是20.2.2021 12:54:34一列还是两列?@Charlieface它是SQLite@IvanKhorin我使用的是一列RecordDateTime。不涉及其他列。@mrwd,如果RecordDateTime是一个类,请提供它的定义。日期是字符串吗?一天中的时间是一个问题吗?你们的数据库管理系统是什么,看起来像SQLite?您使用的是哪种Linq提供程序?是20.2.2021 12:54:34一列还是两列?@Charlieface它是SQLite@IvanKhorin我使用的是一列RecordDateTime。不涉及其他列。@mrwd,如果RecordDateTime是一个类,请提供它的定义。日期是字符串吗?TimeOfDay是一个stirng吗?尽量避免使用DataTable的概念,这是一个噩梦。列出minTime=db.LogModel.AsEnumerable.GroupByx=>x.RecordDateTime.Date.Selectx=>x.Miny=>y.RecordDateTime.TimeOfDay.ToList;这似乎对我的情况有效。经过一些调查,我认为这里的重点是使用。可计算的。我应该提到,我正在使用EFCore从数据库中查询。直接查询和不添加数据一样似乎有一些限制。可计算的一切似乎都在工作。尽量避免使用数据表概念,这是一场噩梦。列出minTime=db.LogModel.AsEnumerable.GroupByx=>x.RecordDateTime.Date.Selectx=>x.Miny=>y.RecordDateTime.TimeOfDay.ToList;这似乎对我的情况有效。经过一些调查,我认为这里的重点是使用。可计算的。我应该提到,我正在使用EFCore从数据库中查询。看起来直接查询和不添加都有一些限制。可计算的一切似乎都在运行。