我正在處理對我來說可能簡單但困難的問題。我正在為停放的汽車拍照,并用車牌保存它們并加蓋時間戳。同一輛車可以在白天被拍到好幾次。
樣本資料
| 盤子 | 品牌 | 模型 | 插入日期 |
|---|---|---|---|
| 99AA111 | 特斯拉 | 小號 | 2022-01-17 04:00:00 |
| 99AA111 | 特斯拉 | 小號 | 2022-01-17 04:30:00 |
| 99AA111 | 特斯拉 | 小號 | 2022-01-17 05:00:00 |
| 59TA3312 | 日產 | 天際線 | 2022-01-17 04:00:00 |
| 59TA3312 | 日產 | 天際線 | 2022-01-17 04:30:00 |
| 129EA512 | 斯巴魯 | 翼豹 | 2022-01-17 03:30:00 |
我想要實作的是;
| 盤子 | 品牌 | 模型 | 第一張照片日期 | 第二張照片日期 |
|---|---|---|---|---|
| 99AA111 | 特斯拉 | 小號 | 2022-01-17 04:00:00 | 2022-01-17 04:30:00 |
| 99AA111 | 特斯拉 | 小號 | 2022-01-17 05:00:00 | - |
| 59TA3312 | 日產 | 天際線 | 2022-01-17 04:00:00 | 2022-01-17 04:30:00 |
| 129EA512 | 斯巴魯 | 翼豹 | 2022-01-17 03:30:00 | - |
我想出了;
var groupedResult = resultList.GroupBy(f => f.Plate).Select(f => new ResultListView
{
Plate = f.Key,
FirstDate = f.Min(f => f.InsertDate),
SecondDate = f.Max(f => f.InsertDate),
Brand = f.FirstOrDefault().Brand,
Model = f.FirstOrDefault().Model,
TimeDifference = (f.Max(f => f.InsertDate) - f.Min(f => f.InsertDate)).TotalMinutes
});
但正如代碼中顯而易見的那樣,它只給了我第一條和最后一條記錄,但并不像我預期的那樣。我正在嘗試按盤子分組,如果同一個盤子不止一次,請將其與下一個相匹配。如果只有一張照片或三張照片,第二個日期應該為空。
當然可以應用不同的邏輯,但我相信這是更清晰的方法。
我認為在分組結果中回圈。拿第一個和第二個,跳過兩個。但這不是我要找的。
謝謝您的幫助。
uj5u.com熱心網友回復:
好吧,有(讓我們使用命名元組來演示):
var resultList = new (string Plate, string Brand, string Model, DateTime InsertDate)[] {
("99AA111", "Tesla", "S", new DateTime(2022,01,17, 04,00,00)),
("99AA111", "Tesla", "S", new DateTime(2022,01,17, 04,30,00)),
("99AA111", "Tesla", "S", new DateTime(2022,01,17, 05,00,00)),
("59TA3312", "Nissan", "Skyline", new DateTime(2022,01,17, 04,00,00)),
("59TA3312", "Nissan", "Skyline", new DateTime(2022,01,17, 04,30,00)),
("129EA512", "Subaru", "Impreza", new DateTime(2022,01,17, 03,30,00)),
};
你可以GroupBy 兩次:
var groupedResult = resultList
.GroupBy(item => item.Plate)
.SelectMany(group => group
.OrderBy(item => item.InsertDate)
.Select((item, index) => (item: item, index: index / 2))
.GroupBy(pair => pair.index)
.Select(g => (
Plate: g.First().item.Plate,
Brand: g.First().item.Brand,
Model: g.First().item.Model,
FirstPhotoDate: g.First().item.InsertDate,
SecondPhotoDate: (g.Count() == 1 ? null : (DateTime?)(g.Last().item.InsertDate))
))
);
我們來看一下:
string report = string.Join(Environment.NewLine, groupedResult
.Select(r => $"{r.Plate,-8} : {r.Brand,-6} : {r.Model,-7} : {r.FirstPhotoDate} : {r.SecondPhotoDate}"));
Console.Write(report);
結果:
99AA111 : Tesla : S : 17.01.2022 4:00:00 : 17.01.2022 4:30:00
99AA111 : Tesla : S : 17.01.2022 5:00:00 :
59TA3312 : Nissan : Skyline : 17.01.2022 4:00:00 : 17.01.2022 4:30:00
129EA512 : Subaru : Impreza : 17.01.2022 3:30:00 :
uj5u.com熱心網友回復:
這是我能想到的最干凈的方法:
var resultList = new (string Plate, string Brand, string Model, DateTime InsertDate)[]
{
("99AA111", "Tesla", "S", new DateTime(2022,01,17, 04,00,00)),
("99AA111", "Tesla", "S", new DateTime(2022,01,17, 04,30,00)),
("99AA111", "Tesla", "S", new DateTime(2022,01,17, 05,00,00)),
("59TA3312", "Nissan", "Skyline", new DateTime(2022,01,17, 04,00,00)),
("59TA3312", "Nissan", "Skyline", new DateTime(2022,01,17, 04,30,00)),
("129EA512", "Subaru", "Impreza", new DateTime(2022,01,17, 03,30,00)),
};
var query =
from r in resultList
group (DateTime?)r.InsertDate by new { r.Plate, r.Brand, r.Model } into grs
from bgrs in grs.OrderBy(x => x).Buffer(2)
select new
{
grs.Key.Plate,
grs.Key.Brand,
grs.Key.Model,
FirstPhotoDate = bgrs.First(),
SecondPhotoDate = bgrs.Skip(1).Any() ? bgrs.Last() : null,
};
我使用了Buffer來自 Microsoft 的System.InteractiveNuGet 的運算子。
這給了我:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/414425.html
標籤:
