c# - 使用 LINQ 过滤两个日期之间的数据

标签 c# .net linq

如何过滤两个日期时间之间的数据。我在这里过滤目录中的文本文件长度。我需要在所选日期之间过滤文本文件。

DateTime startDate = dateTimePicker1.Value;
DateTime endDate = dateTimePicker2.Value;
var queryList1Only = from i in di.GetFiles("*.txt", SearchOption.AllDirectories)  
                     select i.Length;

有什么建议吗?

最佳答案

使用 Where 子句:

DateTime startDate = dateTimePicker1.Value;
DateTime endDate = dateTimePicker2.Value;

var queryList1Only = from i in di.GetFiles("*.txt", SearchOption.AllDirectories) 
                     where i.GetCreationTime() > startDate && i.GetCreationTime() < endDate
                     select i.Length;

您可以使用 GetLastWriteTimeGetLastAccessTime 而不是 GetCreationTime

我建议查看几个使用 where 子句的示例,以全面了解其工作原理 here .

关于c# - 使用 LINQ 过滤两个日期之间的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6199461/

相关文章:

c# - 是什么让 Winform 职位最初变得陈旧?

c# - 在 C# 中编写当单击消息框上的 X 时的代码

c# - 为什么 C# 不允许重载 operator()?

c# - 从单独的线程访问表单的控件

c# - 如何在 IntelliJ Rider 中生成 API Controller ?

.net - 是否应该注入(inject)自定义配置部分依赖项?

c# - 按列表中的多个项目进行分组,然后求和并平均 C#

c# - 为什么Reflector无法显示字节数组?这是由于某些技术限制吗?

c# - Entity Framework 6 Code First 自定义函数

c# - LINQ 查询无法评估 StartsWith 中的 GET 参数