我無法從通過 Dapper 從資料庫獲得的物件串列中創建一個特定的“資料集”。所以事情是關于公司的日常出勤,所以用戶來點擊然后出去點擊,我們有一個像這樣的表格行: sql table 所以我有一個這樣的模型類:
public class Attendance
{
public int UserId { get; set; }
public string FullName { get; set; }
public DateTime DateTimeUtcIn { get; set; }
public DateTime DateTimeUtcOut { get; set; }
public string DayOfTheWeek { get; set; }
public int LocationId { get; set; }
public TimeSpan TotalTime { get; set; }
public byte StatusId { get; set; }
public TimeSpan BreakTime { get; set; }
}
然后在呼叫資料庫后我得到這樣的結果IEnumerable<Attendace>:
var result = await _platformDB.Con.QueryAsync<Models.Attendance.Attendance>(query);
然后我創建了一個像這樣的簡單視圖模型:
public class Time
{
public DateTime DateTimeUtcIn { get; set; }
public DateTime DateTimeUtcOut { get; set; }
}
public class AttendaceViewModel
{
public int UserId { get; set; }
public string FullName { get; set; }
public string Date { get; set; }
public string DayOfTheWeek { get; set; }
public List<Time> TimesInAndOut { get; set; }
public string Locations { get; set; }
public string TotalTime { get; set; } //need to calculate
public string Status { get; set; }
public string BreakTime { get; set; } //need to calculate
}
現在問題來了:如何List<AttendaceViewModel>從按 UserId 分組的結果創建并計算一天的 TotalTime(所有 dateTimeUtcIn dateTimeUtcOut 的總和)和這一天的休息時間,并 List<Time> TimesInAndOut在此處填充?我真的不知道。
uj5u.com熱心網友回復:
我想我知道你需要什么:
List<AttendaceViewModel> resultList = result
.GroupBy(a => (a.UserId, a.FullName, DateIn: a.DateTimeUtcIn.Date, a.DayOfTheWeek, a.LocationId, a.StatusId))
.Select(ag => new AttendaceViewModel
{
UserId = ag.Key.UserId,
FullName = ag.Key.FullName,
Date = ag.Key.DateIn.ToString(),
DayOfTheWeek = ag.Key.DayOfTheWeek,
Locations = ag.Key.LocationId.ToString(),
Status = ag.Key.StatusId.ToString(),
TotalTime = new TimeSpan(ag.Sum(a => (a.DateTimeUtcOut - a.DateTimeUtcIn).Ticks)).ToString(),
TimesInAndOut = ag.Select(a => new Time { DateTimeUtcIn = a.DateTimeUtcIn, DateTimeUtcOut = a.DateTimeUtcOut }).ToList(),
BreakTime = CalcBreaks(ag.Select(a => (a.DateTimeUtcIn, a.DateTimeUtcOut))).ToString()
})
.ToList();
對于BreakTime我使用了這個輔助方法:
private static TimeSpan CalcBreaks(IEnumerable<(DateTime inTime, DateTime outTime)> times)
{
if (!times.Skip(1).Any())
return TimeSpan.Zero;
TimeSpan totalBreakTime = TimeSpan.Zero;
TimeSpan lastLeaveTime = times.First().outTime.TimeOfDay;
foreach(var attendanceTime in times.OrderBy(at => at.inTime).ThenBy(at => at.outTime).Skip(1))
{
TimeSpan breakTime = attendanceTime.inTime.TimeOfDay - lastLeaveTime;
totalBreakTime = breakTime;
lastLeaveTime = attendanceTime.outTime.TimeOfDay;
}
return totalBreakTime;
}
uj5u.com熱心網友回復:
您可能無法僅使用 LINQ 完成此操作。您應該能夠遍歷剛剛獲取的結果中回傳的專案,并在那里執行您的邏輯。
實作這樣的函式,并在檢索結果后呼叫它。如果您愿意,您甚至可以在此函式中處理資料庫查詢,然后只需呼叫該函式并取回您的串列。
private List<AttendanceViewModel> GetAttendanceViewModels((whatever type 'results' is goes here) {type} results)
{
var output = new List<AttendanceViewModel>();
foreach(result in results)
{
var totalTime = CalculateTotalTimeForOneDay(result);
var breakTime = CalculateBreakTime(result);
result.TotalTime = totalTime;
result.BreakTime = breakTime;
}
return output.GroupBy(u => u.UserId)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/463235.html
上一篇:插入SQL到LINQ查詢
