Tag: nullable

如何在Dapper中使用generics和Nullable 类型进行实现?

我有这个电话: public IObservable Subscribe(object topic, string service, string connectionString, string query) { try { this.connection.ConnectionString = connectionString; this.connection.Open(); this.connection.Query(query, new { transactionid = topic }).ToObservable().Subscribe(message => this.subject.OnNext(message)); return this.subject; } catch (Exception e) { this.subject.OnError(e); return this.subject; } finally { this.subject.OnCompleted(); this.connection.Close(); } } 这是我的查询: with IDS as (select L1_ID, L2_ID, L1_VALUE, L2_VALUE from MYDB […]

System.Nullable null * int值的值是多少?

请考虑以下声明: int? v1 = null; int? v2 = 5 * v1; v2的价值是多少? ( null或空字符串?) 如何防止编译器将其标记为无效操作? 我是否需要遵循自定义exception处理?

Null-Coallescing运算符 – 为什么选择铸造?

任何人都可以请告诉我为什么以下第一个语句抛出编译错误而第二个不抛出? NewDatabase.AddInParameter(NewCommand, “@SomeString”, DbType.String, SomeString ?? DBNull.Value); // <– Throws compilation error! NewDatabase.AddInParameter(NewCommand, "@SomeString", DbType.String, (object)(SomeString) ?? DBNull.Value); // <– Compiles! 我尝试过其他可以为空的类型,比如byte? 并得到了相同的结果。 任何人都可以告诉我为什么我需要先投射到物体?

什么是“DateTime?”而不仅仅是C#中的DateTime?

DateTime?区别DateTime? 和C#中的DateTime (没有问号)?

在generics方法中无法将类型更改为可为空

我正在创建一个通用的转换器 以下是通用转换器的示例代码 bool TryReaderParse(object data, out TType value) { value = default(TType); Type returnType = typeof(TType); object tmpValue = null; if (returnType == typeof(DateTime)) { tmpValue = StringToDatetime(data.ToString()); } else if (returnType == typeof(DateTime?)) // THIS IF FIRES { tmpValue = StringToNullableDatetime(data.ToString()); } value = (TType)Convert.ChangeType(tmpValue, returnType); // THROWS } public DateTime? StringToNullableDatetime(string date) { […]

MVC映射到模型中的可空bool

使用包含该字段的视图模型: public bool? IsDefault { get; set; } 尝试在视图中映射时出错: model.IsDefault) %> 无法隐式转换类型’bool?’ ‘bool’。 存在显式转换(您是否错过了演员?) 我已经尝试过使用.Value并且都没有工作。 请注意我想要的行为是提交表单应该将模型中的IsDefault设置为true或false。 值null仅表示尚未填充模型。

不能隐式地将类型’long’转换为“int?”?

这是一个非常简单的问题 – 将类型长变量转换为Nullable int类型(int?)。 (见下面的例子 – 不是实际的代码) int? y = 100; long x = long.MaxValue; y = x; 我收到编译时错误。 “不能隐式地将类型’long’转换为’int?’。存在显式转换(你是否错过了转换?)。 我确实有修复(见下面的解决方案部分),我发布问题的好奇心是推荐的3个解决方案? 解 y = Convert.ToInt32(x); y = (int)x; y = unchecked((int) x); 提前感谢您的建议

在Linq表达式中使用可空类型

var quantSubset = from userAns in userAnalysis.AllUserAnswers join ques in userAnalysis.AllSeenQuestions on userAns.QID equals ques.QID where (ques.QuestionType == “QT”) select new { QuestionLevel = ques.LevelID, TimeTaken = userAns.TimeTaken, Points = userAns.Points, UsedWeapon = (userAns.UsedBy2 && userAns.UsedHint), WasCorrect = userAns.WasCorrect.HasValue ? userAns.WasCorrect.Value : null }; 在我的选择表达式中,我想选择一个可以为空的类型WasCorrect(表达式的最后一部分),但显然我不能按照我目前的方式来做。 如何将WasCorrect作为可空类型 我试过?WasCorrect但是在Visual Studio中也没有给出错误。

将null转换为某些东西?

今天我和一位同事进行了这次有趣的讨论。 我们在C#中讨论两段代码。 代码片段1: if(!reader.IsDBNull(2)) { long? variable1 = reader.GetInt64(2) } 代码片段2: long variable1 = reader.IsDBNull(2) ? (long?) null : reader.GetInt64(2) 问题是:将null转换为可空的长度是一个好习惯吗? 或者你宁愿使用传统的if语句来避免将null为可空的long。

Nullable 类型有什么用?

布尔变量可以保持真或假,而布尔? 也可以为空。 为什么我们需要bool的第三个值? 如果它不是真的 ,它是什么,它是== false 你能提出一个我喜欢布尔的场景吗? 代替。 谢谢