nullable

Nullable DateTime in C#

北城以北 提交于 2021-01-28 18:21:14
问题 I have two questions related to DateTime assingments DateTime? y = 1 == 1 ? null: DateTime.MaxValue; DateTime? y = null; // assignment works as expected Why the first assignment issues error of type conversion between null and DateTime? Which is the preferred way for null assignments of DateTime? in c#. DateTime? x = default(DateTime?); //prints null on console DateTime? x = null; // prints null on console DateTime? x = DateTime.MinValue; //print 01/01/0001 回答1: The second statement DateTime?

Comparing non nullable `int` to `null` (LINQ)

怎甘沉沦 提交于 2021-01-27 17:06:12
问题 I have EF model class and I've decided to extend that class with one bool property: class A { public int Id { get; set; } public string Value { get; set; } } class A_DTO : A { public bool BoolProp { get; set; } } class C { public int Id { get; set; } public int A_Id { get; set; } } Then I wrote a method, which will return that A collection joined with some other C collection, which contains A <=> C mapping (well, in real world example it contains some SystemId and linq query will be joined by

How to solve “The issue with T?”/nullable constraint on type parameter?

久未见 提交于 2020-12-05 11:10:50
问题 I'm designing an interface in C# 8.0 with nullable enabled, targeting .Net Standard 2.0 (using the Nullable package) and 2.1. I am now facing The issue with T? . In my example, I am building an interface for a cache which stores Stream s/byte data, identified by a string key, i.e. the file system could by a trivial implementation. Every entry is additionally identified by a version, which should be generic. This version could for example be another string key (like an etag), an int or a date

Write a prepared statement with nullable values in conditions

浪子不回头ぞ 提交于 2020-11-24 18:02:47
问题 Is there a way to write a prepared statement where a value is compared to another value in a condition and I don't know, whether this value is NULL or not. SELECT `foo` FROM `bar` WHERE `a1` = :a1 AND `a2` = :a2 If I would use this prepared statement with a1 => null and a2 => 42 , then the resulting query would be: SELECT `foo` FROM `bar` WHERE `a1` = NULL AND `a2` = '42' This is, of course, not what I want. I would need this in that case: SELECT `foo` FROM `bar` WHERE `a1` IS NULL AND `a2` =

Write a prepared statement with nullable values in conditions

假装没事ソ 提交于 2020-11-24 17:57:15
问题 Is there a way to write a prepared statement where a value is compared to another value in a condition and I don't know, whether this value is NULL or not. SELECT `foo` FROM `bar` WHERE `a1` = :a1 AND `a2` = :a2 If I would use this prepared statement with a1 => null and a2 => 42 , then the resulting query would be: SELECT `foo` FROM `bar` WHERE `a1` = NULL AND `a2` = '42' This is, of course, not what I want. I would need this in that case: SELECT `foo` FROM `bar` WHERE `a1` IS NULL AND `a2` =