从匹配字符串最近日期时间参数的列表中删除项目
本文关键字:参数 列表 删除项目 时间 日期 字符串 最近 | 更新日期: 2023-09-27 18:14:14
我正试图缩小一个列表,其中包括所有文件与匹配的产品ID(如M320.1215)。当我说我需要缩小范围时,我想要删除列表条目,以便只保留列表中最近的条目。
这是一个文件名的示例:I_ATTRIBUTES_M320.1215_Eli_CS_07112016225939.xlsx
这里你可以看到产品Id为"M320.1215"子格式和语言"Eli_CS"日期和时间07112016225939格式为MMDDYYYYHHMMSS。我可以得到日期时间到DateTime对象使用:
public DateTime correctedDateString(string dts)
{
string correctDTS = dts.Insert(2, "/");
correctDTS = correctDTS.Insert(5, "/");
correctDTS = correctDTS.Insert(10, " ");
correctDTS = correctDTS.Insert(13, ":");
correctDTS = correctDTS.Insert(16, ":");
DateTime convertedDate = DateTime.Now;
try
{
convertedDate = Convert.ToDateTime(correctDTS);
Console.WriteLine("'{0}' converts to {1} {2} time.", correctDTS, convertedDate, convertedDate.Kind.ToString());
}
catch (FormatException)
{
convertedDate = Convert.ToDateTime("01/01/2015 00:00:00");
Console.WriteLine("'{0}' is not in the proper format.", correctDTS);
}
return convertedDate;
这显然是一个简单的方法。我一直在使用以下方法将列表中的项目分成可用的部分:
string[] tempArray = Path.GetFileNameWithoutExtension(filenames[i].ToString()).ToString().Split(new[] { "_" }, StringSplitOptions.None);
现在我正在努力操作下面的列表,以只保留每个子格式和语言组合的最新版本。
List<string> filenames = new List<string>()
{
"I_ATTRIBUTES_M320.1215_EGHS_RU_07132016020215",
"I_ATTRIBUTES_M320.1215_EGHS_BE_06292016132122",
"I_ATTRIBUTES_M320.1215_EGHS_BE_06302016100039",
"I_ATTRIBUTES_M320.1215_EGHS_BE_07042016080530",
"I_ATTRIBUTES_M320.1215_EGHS_BE_07112016225936",
"I_ATTRIBUTES_M320.1215_EGHS_BE_07132016020203",
"I_ATTRIBUTES_M320.1215_EGHS_BR_06292016132127",
"I_ATTRIBUTES_M320.1215_EGHS_BR_06302016100042",
"I_ATTRIBUTES_M320.1215_EGHS_BR_07042016080536",
"I_ATTRIBUTES_M320.1215_EGHS_BR_07112016225938",
"I_ATTRIBUTES_M320.1215_EGHS_BR_07132016020206",
"I_ATTRIBUTES_M320.1215_EGHS_CS_07112016225939",
"I_ATTRIBUTES_M320.1215_EGHS_CS_07132016020207",
"I_ATTRIBUTES_M320.1215_EGHS_DE_06292016132128",
"I_ATTRIBUTES_M320.1215_EGHS_DE_06302016100044",
"I_ATTRIBUTES_M320.1215_EGHS_DE_07042016080537",
"I_ATTRIBUTES_M320.1215_EGHS_DE_07112016225940",
"I_ATTRIBUTES_M320.1215_EGHS_DE_07132016020208",
"I_ATTRIBUTES_M320.1215_EGHS_FR_06292016132129",
"I_ATTRIBUTES_M320.1215_EGHS_FR_06302016100045",
"I_ATTRIBUTES_M320.1215_EGHS_FR_07042016080538",
"I_ATTRIBUTES_M320.1215_EGHS_FR_07112016225941",
"I_ATTRIBUTES_M320.1215_EGHS_FR_07132016020210",
"I_ATTRIBUTES_M320.1215_EGHS_IT_06292016132129",
"I_ATTRIBUTES_M320.1215_EGHS_IT_06302016100046",
"I_ATTRIBUTES_M320.1215_EGHS_IT_07042016080539",
"I_ATTRIBUTES_M320.1215_EGHS_IT_07112016225941",
"I_ATTRIBUTES_M320.1215_EGHS_IT_07132016020211",
"I_ATTRIBUTES_M320.1215_EGHS_MS_06292016132130",
"I_ATTRIBUTES_M320.1215_EGHS_MS_06302016100047",
"I_ATTRIBUTES_M320.1215_EGHS_MS_07042016080540",
"I_ATTRIBUTES_M320.1215_EGHS_MS_07112016225943",
"I_ATTRIBUTES_M320.1215_EGHS_MS_07132016020212",
"I_ATTRIBUTES_M320.1215_EGHS_PL_06292016132131",
"I_ATTRIBUTES_M320.1215_EGHS_PL_06302016100048",
"I_ATTRIBUTES_M320.1215_EGHS_PL_07042016080541",
"I_ATTRIBUTES_M320.1215_EGHS_PL_07112016225944",
"I_ATTRIBUTES_M320.1215_EGHS_PL_07132016020214",
"I_ATTRIBUTES_M320.1215_EGHS_RU_06292016132131",
"I_ATTRIBUTES_M320.1215_EGHS_RU_06302016100049",
"I_ATTRIBUTES_M320.1215_EGHS_RU_07042016080542",
"I_ATTRIBUTES_M320.1215_EGHS_RU_07112016225945"
};
所以实际上我需要的最终列表是这样的:
List<string> filenames = new List<string>()
{
"I_ATTRIBUTES_M320.1215_EGHS_BE_07132016020203",
"I_ATTRIBUTES_M320.1215_EGHS_BR_07132016020206",
"I_ATTRIBUTES_M320.1215_EGHS_CS_07132016020207",
"I_ATTRIBUTES_M320.1215_EGHS_DE_07132016020208",
"I_ATTRIBUTES_M320.1215_EGHS_FR_07132016020210",
"I_ATTRIBUTES_M320.1215_EGHS_IT_07132016020211",
"I_ATTRIBUTES_M320.1215_EGHS_MS_07132016020212",
"I_ATTRIBUTES_M320.1215_EGHS_PL_07132016020214",
"I_ATTRIBUTES_M320.1215_EGHS_RU_07132016020215"
};
提前感谢您的帮助
你可以用Linq
var grouped = filenames.Select(x => x.Split('_'))
.GroupBy(x => x[2] + x[3] + x[4], p => p, (key, g) => new { Id = key, Items = g.ToList() })
.Select(x => x.Items.OrderByDescending(i => correctedDateString(i[5])).FirstOrDefault())
.Select(x => string.Join("_", x))
.ToList();
试试这个:
var result = filenames.Select(s =>
{
var splitted = s.Split('_');
return new
{
ProductId = splitted[2],
Subformat = splitted[3],
Language = splitted[4],
DateTime = DateTime.ParseExact(splitted[5], "MMddyyyyHHmmss", null),
Source = s
};
})
.GroupBy(a => new { a.ProductId, a.Subformat, a.Language })
.Select(g => g.First(a => a.DateTime == g.Max(b => b.DateTime)).Source)
.ToList();
我用的是DataTime.ParseExact
方法,而不是你的correctedDateString
方法。
您可以使用List<T>.Find(Predicate<T>)
方法找到特定的项目,使用List<T>.Remove(T)
方法删除选中的项目。
的例子:
// Find an Employee by their ID.
Employee result = Employees.Find(
delegate(Employee emp)
{
return emp.ID == IDtoFind;
}
);