当日期细分为 2 个整数(月和月)时,按日期过滤的最佳方法是什么?年?

发布于 2024-09-30 15:23:15 字数 225 浏览 1 评论 0原文

我有一个数据表,其中的日期按月份和日期划分。年。我需要按日期过滤,因此我正在考虑添加带有表达式的列以获得真正的日期时间列。这是处理这个问题的最好方法吗? “转换”可以做到这一点吗?像...

dt.Columns.Add("RealDate", typeof(DateTime), "Convert(Month + '/1/' + Year, 'System.DateTime')");

I have a DataTable that has a date broken up by Month & Year. I need to filter by date so I was thinking about adding a column with an expression to get a true DateTime column. Is this the best way handle this? Can 'Convert' do this? Something like...

dt.Columns.Add("RealDate", typeof(DateTime), "Convert(Month + '/1/' + Year, 'System.DateTime')");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原野 2024-10-07 15:23:15

我将在填充数据表的 SQL 中执行转换。这可能是内联 SQL、存储过程或 ORM。

让数据库引擎完成工作。不要花费大量资源在应用中执行这些转换和计算。

编辑:如果无法更改生成 DataTable 的过程,则可以使用 DataView RowFilter 进行过滤,如下所示:

DataView dataView = dataTable1.DefaultView;
dataView.RowFilter = string.Format("Month = '{0}' && Year = '{1}'", Month, Year);

这是一个很棒的示例页面,包含演示和代码

I would perform the transformation in the SQL that populates the DataTable. That might be inline SQL, a stored procedure, or an ORM.

Let the database engine do the work. Don't spend a lot of resource performing these transformations and calculations in your app.

Edit: If you can't alter the process generating the DataTable, you can filter by using a DataView RowFilter, like this:

DataView dataView = dataTable1.DefaultView;
dataView.RowFilter = string.Format("Month = '{0}' && Year = '{1}'", Month, Year);

Here's a terrific page of examples, with demos and code.

深巷少女 2024-10-07 15:23:15

由于没有人回答,我将重申我使用的代码:

dt.Columns.Add("RealDate", typeof(DateTime), "Convert(Month + '/1/' + Year, 'System.DateTime')");

Since no one answered, I'll reiterate the code I used:

dt.Columns.Add("RealDate", typeof(DateTime), "Convert(Month + '/1/' + Year, 'System.DateTime')");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文