- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
C#
中是否有 not null coalescing 运算符,以防万一可以使用,例如:
public void Foo(string arg1)
{
Bar b = arg1 !?? Bar.Parse(arg1);
}
下面这个案例让我想到了:
public void SomeMethod(string strStartDate)
{
DateTime? dtStartDate = strStartDate !?? DateTime.ParseExact(strStartDate, "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);
}
我可能没有strStartDate
信息,万一它会是null
但如果我有;我始终确信它将采用预期的格式。因此,不是初始化 dtStartDate = null
并尝试 parse
并在 try catch
block 中设置值。好像比较有用。
我想答案是否定的(并且没有这样的运算符 !??
或其他任何东西)我想知道是否有一种方法可以实现这个逻辑,它是否值得,以及它在什么情况下有用。
最佳答案
Mads Torgersen 公开表示正在考虑为下一版本的 C# 添加空值传播运算符(但同时强调这并不意味着它会存在)。这将允许像这样的代码:
var value = someValue?.Method()?.AnotherMethod();
如果操作数(左侧)为 null
,则 ?.
返回 null
,否则将计算右侧。我怀疑这会给你带来很多帮助,特别是如果与(比如)扩展方法结合使用;例如:
DateTime? dtStartDate = strStartDate?.MyParse();
哪里:
static DateTime MyParse(this string value) {
return DateTime.ParseExact(value, "dd.MM.yyyy",
System.Globalization.CultureInfo.InvariantCulture
);
但是!只需使用扩展方法,您现在就可以做同样的事情:
DateTime? dtStartDate = strStartDate.MyParse();
static DateTime? MyParse(this string value) {
if(value == null) return null;
return DateTime.ParseExact(value, "dd.MM.yyyy",
System.Globalization.CultureInfo.InvariantCulture
);
关于c# - 有没有办法实现和使用 "NOT null coalescing"运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21425422/
我正在尝试返回有关所执行搜索次数的每小时报告。我的结果不包括搜索为零的时间,我认为我的语法正确,可以使用 COALESCE。谁能看到我做错了什么?谢谢 SELECT CAST(startdatetim
众所周知,COALESCE 是一个 ANSI SQL 标准函数。它在不同的 RDBMS 中具有相同的功能(即)它返回值列表中的第一个 NOT NULL 值。 考虑以下数据设置 CREATE TABLE
## 问题## 我从这个脚本中收到错误(Postgresql 9.3.2) (在 MS SQL Server 中没问题) SELECT CASE COALESCE(my_date_field,0)
只是想看看是否有人有更好的方法来完成我所需要的。 先来个背景。我的数据库中有一个表,目前大约有 20,000 行。在表中,我们有一列用于 FirstName 和 LastName。还有一列是 Refe
我在 SO 中找不到它,并且认为在这里找到它可能是值得的,因为 oracle 文档没有指定它。 http://docs.oracle.com/cd/B28359_01/server.111/b2828
我刚刚了解了 COALESCE,我想知道是否可以在两个表之间合并整行数据?如果没有,以下散漫的最佳方法是什么? 例如,我有这两个表并假设所有列都匹配: tbl_员工 Id Name E
为什么此查询在某些情况下会产生重复项? Table_1 ID 1 2 3 Table_2 ID 1 2 4 Table_3 ID 1 3 4 询问: SELECT C
我有一张名为 table1 的表带列Gender varchar(10)和 Team numeric . create table table1 ( ID integer Gender varchar
SQL Server 2005 中是否有一个函数,如果任何参数(任何类型)为 NULL,它会返回 NULL [或 bool 值],这样我就不用编写 IF a 为 NULL 或 b 为 NULL 或 c
来自this question , a neat answer about using COALESCE简化复杂的逻辑树。我考虑了短路的问题。 例如,在大多数语言的函数中,参数都会被完全求值,然后传递
// lItems is TDataSet or TClientDataSet with lItems do begin Close; Filtered := false; Fi
我有一个包含季度值的表,我需要添加一个新列,为我提供上个季度的最后一个非空值。例如 ID | Project | Q1 | Q2 | Q3 | Q4 | Current Quarte
大家好,Stackoverflowers!我有一个正在处理的查询,我需要过滤掉 0,但它没有删除那些记录,我似乎无法弄清楚原因。 请看下面, SELECT disb_sum, pl_balance,
我是 mysql 触发器的新手。只是让我的头脑思考一些事情,以便我了解某些内容是否可能未正确编码或最有效的路线:) 我想做的是在添加数据库条目后通过提交的值连接表以创建用于全文搜索的文本字符串,因为我
我是 REDSHIFT 中 COALESCE 功能的新手。我在 mysql 和 Redshift 中运行了以下四个查询。 第一个和第二个查询在 mysql 和 redshift 中均按预期执行。但对于
首先,我想澄清一下,我已经阅读过 this问题。那里描述的问题与我的问题非常相似,但与一个错误有关,该错误已经解决。 通过在 Windows 上运行的 MySQL WorkBench 5.2.47 C
我有这两张表: users(这有一个 updated_at 列)和 user_updates(这有一个用户的外键 user_id 和一个 created_at 列) 我想为用户找到最新的用户更新,所以
我正在使用 Postgres 并有以下 SQL 语句: SELECT * FROM "osmlocal-dsd-de".t_osm_vehicle_image t WHERE t.vehicle_co
如果我有一个简单的查询 SELECT row FROM table WHERE id=my_id 如果找不到 my_id,我想要一行 NULL 而不是空行,我可以使用 coalesce COALESC
我有一个使用 COALESCE(timestamp_type::date,charachter_varying) 的查询,由于数据类型不匹配而失败: 错误:无法匹配 COALESCE 类型的日期和字符
我是一名优秀的程序员,十分优秀!