为数据表中的计算列定义 DateDiff

Defining DateDiff for a calculated column in a datatable(为数据表中的计算列定义 DateDiff)
本文介绍了为数据表中的计算列定义 DateDiff的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 DateTimeExpired 列,我想创建另一个名为Expired"的列,它将根据到期日期显示是"或否" - 如果日期已经过去,则显示是".
我是这样写的:

I have the column DateTimeExpired, and I would like to create another column called "Expired" which will show "Yes" or "No" according to the expiration date - "Yes" if the date has already passed.
I wrote this:

DataColumn colExpirationDate = new DataColumn("DateTimeExpired", typeof(DateTime));
DataColumn colExpired = new DataColumn("Expired", typeof(string), "IIF(DateDiff(DateTimeExpired, date())>= 0,'No','Yes')");

但我得到一个异常表达式包含未定义的函数调用 DateDiff()."

But I get an exception "The expression contains undefined function call DateDiff()."

(请注意,我总是想获取该行,无论它是否过期)

(please note that I always want to get the row, no matter if it's expired or not)

如何将列的文本设置为正确的形式?

How do I set the column's text to the correct form?

推荐答案

正如 MA Hanin 在评论中指出的那样,DateDiff 似乎不能在 数据列表达式.您可以尝试将计算列构建到基础表中(如果您使用的是 MS Sql 或类似的)

As M.A Hanin pointed out in his comment, it looks like DateDiff cannot be used in DataColumn Expressions. You could try building the calculated column into the underlying table instead (if you are using MS Sql or similar)

没有获取今天"的功能,但假设您添加的 DataColumn 仅存在几个小时,您可以将今天的日期构建为常量,然后使用比较运算符而不是 DateDiff

edit: There is no function to get 'today', but assuming that the DataColumn you are adding will only exist for a few hours, you could build in todays date as a constant, and then use comparison operators instead of DateDiff

试试这个:

DataColumn colExpirationDate = new DataColumn("DateTimeExpired", typeof(DateTime));
DataColumn colExpired = new DataColumn("Expired", typeof(string),
    String.Format("IIF(DateTimeExpired > #{0}#,'No','Yes')",
    DateTime.Now.ToString("dd/MMM/yyyy")));

请注意,这仅在您的 DataColumn 仅在内存中保留不到一天时才有效.

Note this will only work if your DataColumn is only retained in memory for less than a day.

这篇关于为数据表中的计算列定义 DateDiff的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)