在数据表中定义则DateDiff为计算列 [英] Defining DateDiff for a calculated column in a datatable

查看:527
本文介绍了在数据表中定义则DateDiff为计算列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我列DateTimeExpired,我想创建一个名为过期,这将显示是或否根据截止日期的另一列 - 是的,如果日期已过
我写这样的:

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')");

不过,我得到一个异常的EX pression包含未定义的函数调用则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?

推荐答案

麻HANIN指出,在他的评论,看起来则DateDiff不能在的 DataColumn的前pressions 的。你可以尝试建立计算列到下面的表中,而不是(如果你使用的是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为计算列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆