使用匹配字符串参数从列表中删除项目最近日期时间

时间:2016-10-20 16:02:35

标签: c# list datetime

我正在尝试缩小列表,该列表由具有匹配产品ID的所有文件组成(例如M320.1215)。当我说我需要缩小它时,我想删除列表条目,以便只保留列表中的最新项目。

这是文件名的示例:I_ATTRIBUTES_M320.1215_EGHS_CS_07112016225939.xlsx

在这里,您将产品ID视为“M320.1215” 子格式和语言“EGHS_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"
         };

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

您可以使用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();

答案 1 :(得分:0)

您可以使用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;
    }
);

答案 2 :(得分:0)

试试这个:

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方法。