C# 如何对存储在列表中的数据进行排序和输出

C# 如何对存储在列表中的数据进行排序和输出,c#,linq,sorting,loops,timespan,C#,Linq,Sorting,Loops,Timespan,我有一个应用程序,它读取带有日期的文本文件 请注意,我仍在学习C# 文本文件中的内容如下所示: 即内容未排序,但我已将其存储在列表中并按ASC顺序排序。 02/10/1998 03/10/1998 07/10/1998 10/10/1998 17/10/1998 20/10/1998 这个文本文件有上百个日期,因为我用的是手机,所以我不能输入所有的文本,也不能写完整的代码 基本上,应用程序的目的是写入一个新的文本文件,其中包含timespan中**周块的内容 即: 输出应始终从第一个星期日到星

我有一个应用程序,它读取带有日期的文本文件

请注意,我仍在学习C#

文本文件中的内容如下所示: 即内容未排序,但我已将其存储在列表中并按ASC顺序排序。

02/10/1998
03/10/1998
07/10/1998
10/10/1998
17/10/1998
20/10/1998
这个文本文件有上百个日期,因为我用的是手机,所以我不能输入所有的文本,也不能写完整的代码

基本上,应用程序的目的是写入一个新的文本文件,其中包含timespan中**周块的内容 即:

输出应始终从第一个星期日到星期六开始,并以最后一个星期日到星期六结束

我所需要知道的就是如何从时间跨度中通过周块生成这样的输出

我已经编写了从列表排序的代码,现在我想我必须解析日期格式

var sortedlist = stulist.OrderBy(s => s.DOB);

DateTime dt = DateTime.ParseExact(line, "ddddMMMMdd", CultureInfo.InvariantCulture);
DateTime dt2 = DateTime.ParseExact(line, "dddMMMd", CultureInfo.InvariantCulture);
DateTime dt3 = DateTime.ParseExact(line, "MM/dd/yyyy", CultureInfo.InvariantCulture);
我想我必须循环,但我不知道在我的while循环中指定什么

我们将非常感谢您的帮助

有什么区别呢

tx.WriteLine(dt.ToString("dddd dd yyyy", CultureInfo.InvariantCulture));
如果我能保持准确,你有什么意见吗

tx.WriteLine(dt);

你的问题有点让人困惑。我试着重述一下你可能正在做的事情。您似乎有一个
学生
班级(除其他外)有一个字段
DOB
。我假设它的类型是
DateTime
。然后,您必须执行以下操作才能读取文本文件:

var students = new List<Student>();
string[] lines = File.ReadAllLines("my file path");
CultureInfo southAfricanCuture = new CultureInfo("en-ZA");
foreach (string l in lines) {
    DateTime d;
    if (DateTime.TryParseExact(l, "dd/MM/yyyy", southAfricanCuture,
                               DateTimeStyles.AllowWhiteSpaces, out d))
    {
        students.Add(new Student { DOB = d });
    }
}
现在,让我们使用LINQ创建日期块(确保在代码开头有一个
using System.LINQ;
):

现在,让我们创建输出:

foreach (var block in dateBlocks) {
    // Week header
    DateTime firstDayOfWeek = FirstDateOfWeek(block.First());
    DateTime lastDayOfWeek = firstDayOfWeek.AddDays(6);
    string weekHeader = String.Format(southAfricanCuture,
        "Week : {0:dddd dd/MM yyyy} to {1:dddd dd/MM yyyy}",
        firstDayOfWeek, lastDayOfWeek);
    Console.WriteLine(weekHeader);

    // Week details.
    // If the dates appear in wrong order in the input file,
    // order the days within the groups.
    foreach (DateTime date in block.OrderBy(d => d)) {
        Console.WriteLine(date.ToString("    dddd dd/MM yyyy", southAfricanCuture));
        Console.WriteLine(date.ToString("    ddd d/M yy", southAfricanCuture));
        Console.WriteLine(date.ToString("    dd/MM/yy", southAfricanCuture));
    }
}
Console.ReadKey();

正如您所见,处理日期是一件复杂的事情。

在这个qwestion中有一个linq标记。我尝试在linq中这样做

创建具有值的列表

    var lst = new List<string> {"02/10/1998",
    "03/10/1998",
    "07/10/1998",
    "10/10/1998",
    "17/10/1998",
    "20/10/1998"
    };
让我们的列表更有用

List<MyClass> lst3 = DateList.Select(p => new MyClass
        {
            DT = p,
            WeekOfYear = CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(p , DateTimeFormatInfo.CurrentInfo.CalendarWeekRule, DayOfWeek.Monday),
            FirstDayOfWeek = p.AddDays(DayOfWeek.Monday - p.DayOfWeek),
            LastDayOfWeek = p.AddDays(DayOfWeek.Sunday - p.DayOfWeek)
        }).OrderBy(x=>x.WeekOfYear).ToList();

您应该编辑您的问题,并澄清您实际上想知道如何从时间跨度中获取这些周块。目前还不清楚你的问题的核心是这个还是日期时间格式。@TimSchmelter谢谢你通知我,我已经编辑了。你想要所有的星期块还是只有那些你也有日期要显示的块?@T_D我应该只有那些我必须显示的。谢谢,这是我一直在寻找的,所以基本上我应该使用date.ToString作为我的输出。这应该行得通,在我发布这个问题的时候,我是一个初学者,当它出现时,LINQ。@Brian02这是什么意思?这个答案对你有帮助吗?
foreach (var block in dateBlocks) {
    // Week header
    DateTime firstDayOfWeek = FirstDateOfWeek(block.First());
    DateTime lastDayOfWeek = firstDayOfWeek.AddDays(6);
    string weekHeader = String.Format(southAfricanCuture,
        "Week : {0:dddd dd/MM yyyy} to {1:dddd dd/MM yyyy}",
        firstDayOfWeek, lastDayOfWeek);
    Console.WriteLine(weekHeader);

    // Week details.
    // If the dates appear in wrong order in the input file,
    // order the days within the groups.
    foreach (DateTime date in block.OrderBy(d => d)) {
        Console.WriteLine(date.ToString("    dddd dd/MM yyyy", southAfricanCuture));
        Console.WriteLine(date.ToString("    ddd d/M yy", southAfricanCuture));
        Console.WriteLine(date.ToString("    dd/MM/yy", southAfricanCuture));
    }
}
Console.ReadKey();
    var lst = new List<string> {"02/10/1998",
    "03/10/1998",
    "07/10/1998",
    "10/10/1998",
    "17/10/1998",
    "20/10/1998"
    };
var DateList = (from value in lst
                   select DateTime.ParseExact(value, "dd/MM/yyyy",
                       CultureInfo.InvariantCulture)).ToList();
List<MyClass> lst3 = DateList.Select(p => new MyClass
        {
            DT = p,
            WeekOfYear = CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(p , DateTimeFormatInfo.CurrentInfo.CalendarWeekRule, DayOfWeek.Monday),
            FirstDayOfWeek = p.AddDays(DayOfWeek.Monday - p.DayOfWeek),
            LastDayOfWeek = p.AddDays(DayOfWeek.Sunday - p.DayOfWeek)
        }).OrderBy(x=>x.WeekOfYear).ToList();
 int current_week=-1;
        foreach (MyClass o in lst3)
        {
            if (current_week > o.WeekOfYear)
            {
                current_week = o.WeekOfYear;
                Console.WriteLine("Week: {0} {1} to {2} {3}", o.FirstDayOfWeek.DayOfWeek.ToString() ,o.FirstDayOfWeek, o.LastDayOfWeek.DayOfWeek.ToString(), o.LastDayOfWeek);
            }
            Console.WriteLine("{0} {1}", o.DT.DayOfWeek.ToString(), o.DT);
        }