nullable

C# Nullable arrays

偶尔善良 提交于 2019-12-23 06:57:01
问题 I have a search function, but I would like LocationID to be an array of integers rather than just a single integer. I'm not sure how to do this since I want it to also be nullable. I've looked at doing int?[] but then I'd have to check the HasValue of every single entry. Is there a better way? This is what I currently have: public ActionResult Search(string? SearchString, int? LocationId, DateTime? StartDate, DateTime? EndDate) 回答1: Arrays are always reference types, as is string - so they're

Rendering hidden form field using @Html.HiddenFor

◇◆丶佛笑我妖孽 提交于 2019-12-23 04:09:27
问题 public class SearchForm { //Note: Property is nullable public DateTime? CurrentViewDate {get;set;} public DateTime StartDate {get;set;} } //In the controller //[GET] public ActionResult Index() { } //[POST] public ActionResult Index(SearchForm formModel) { if(formModel.CurrentViewDate == null) formModel.CurrentViewDate = DateTime.Now.AddDays(1d); else formModel.CurrentViewDate = formModel.CurrentViewDate.AddDays(1d); formModel.StartDate = DateTime.Now; } //In view @Html.HiddenFor(c => c

Rendering hidden form field using @Html.HiddenFor

时光怂恿深爱的人放手 提交于 2019-12-23 04:09:10
问题 public class SearchForm { //Note: Property is nullable public DateTime? CurrentViewDate {get;set;} public DateTime StartDate {get;set;} } //In the controller //[GET] public ActionResult Index() { } //[POST] public ActionResult Index(SearchForm formModel) { if(formModel.CurrentViewDate == null) formModel.CurrentViewDate = DateTime.Now.AddDays(1d); else formModel.CurrentViewDate = formModel.CurrentViewDate.AddDays(1d); formModel.StartDate = DateTime.Now; } //In view @Html.HiddenFor(c => c

Using nullable types in Linq expressions

穿精又带淫゛_ 提交于 2019-12-22 08:45:25
问题 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 }; In my select expression I want to select a nullable type WasCorrect (last part of the expression)

How is Nullable<T> different from a similar custom C# struct?

倖福魔咒の 提交于 2019-12-21 18:13:15
问题 In Nullable micro-optimizations, part one, Eric mentions that Nullable<T> has a strange boxing behaviour that could not be achieved by a similar user-defined type. What are the special features that the C# language grants to the predefined Nullable<T> type? Especially the ones that could not be made to work on a MyNullable type. Of course, Nullable<T> has special syntactic sugar T? , but my question is more about semantics. 回答1: What I was getting at is: there is no such thing as a boxed

How is Nullable<T> different from a similar custom C# struct?

你。 提交于 2019-12-21 18:13:06
问题 In Nullable micro-optimizations, part one, Eric mentions that Nullable<T> has a strange boxing behaviour that could not be achieved by a similar user-defined type. What are the special features that the C# language grants to the predefined Nullable<T> type? Especially the ones that could not be made to work on a MyNullable type. Of course, Nullable<T> has special syntactic sugar T? , but my question is more about semantics. 回答1: What I was getting at is: there is no such thing as a boxed

How to correctly deal with System.Nullable<T> fields of LinqToSql classes?

為{幸葍}努か 提交于 2019-12-21 16:52:56
问题 I've got a table with some nullable double fields. Working with LinqToSQL trying to use the field directly I get Argument type System.Nullable is not assignable to parameter type double How do I deal with this correctly? 回答1: The problem has nothing to do with Linq. It's got to do with conversion between double and double? / Nullable<double> . Implicit conversion from double? to double is not allowed: double? foo ; double bar = foo ; You can reference the double value directly: double? foo ;

How do you add a NOT NULL FOREIGN KEY column to an existing (populated) table in MS SQL?

强颜欢笑 提交于 2019-12-21 09:36:37
问题 I need to add a NOT NULL column to an existing (populated) table that will be a foreign key to another table. This brings about two problems: When you add the column, its value cannot be null - using a default is not an option (unless it is removed later on) because the database logic is used in the server side validation when a user enters a new record i.e. when the application tries to add a record with this field having a null value, the database throws an error that the application

Why can't nullables be declared const?

孤街醉人 提交于 2019-12-21 07:34:09
问题 [TestClass] public class MsProjectIntegration { const int? projectID = null; // The type 'int?' cannot be declared const // ... } Why can't I have a const int? ? Edit: The reason I wanted a nullable int as a const is because I'm just using it for loading some sample data from a database. If it's null I was just going to initialize sample data at runtime. It's a really quick test project and obviously I could use 0 or -1 but int? just felt like the right data structure for what I wanted to do.

I'm getting Overload resolution ambiguity error on kotlin safe call

浪子不回头ぞ 提交于 2019-12-21 07:25:23
问题 I have a nullable string variable ab . If I call toUpperCase via safe call operator after I assign null to it, kotlin gives error. fun main(args: Array<String>){ var ab:String? = "hello" ab = null println(ab?.toUpperCase()) } Error:(6, 16) Overload resolution ambiguity: @InlineOnly public inline fun Char.toUpperCase(): Char defined in kotlin.text @InlineOnly public inline fun String.toUpperCase(): String defined in kotlin.text What's the problem here? 回答1: As stated in this doc about smart