nullable

DateTime comparison in ObjectQuery.Where

I'm using Entity Framework, and I have a COMMENT entity. A COMMENT has a DATEMODIFIED property, which is a Nullable Date. I'm trying to build a query that will filter COMMENTs by date, so I create a startDate object, and do the following: Dim q As ObjectQuery(Of COMMENT) = _ (From c In model.COMMENT Select c) If startDate.HasValue...

Parsing boolean from configuration section in web.config

I have a custom configuration section in my web.config. One of my classes is grabbing from this: <myConfigSection LabelVisible="" TitleVisible="true"/> I have things working for parsing if I have true or false, however if the attribute is blank I am getting errors. When the config section tries to map the class to the configuration s...

Populating an Object from the DB -- Where do you stop?

When getting an object from the DB, should the object's properties also be loaded? There seems to be these approaches. Create well-formed, fully-loaded objects. Pro: No need to check if a property has been loaded; it has. Pass it around and don’t worry about parts of the object not being there. Con: Where do you stop? If an object h...

C#: passing nullable variable to method that only accepts nonnull vars

I have code that is similar to this: public xyz (int? a) { if (a.HasValue) { // here DoSomething has parameters like DoSomething(int x) blah = DoSomething(a); I am getting the error (cannot convert from int? to int). Is there a way I can pass the variable 'a' to my function without having to do DoSomething(int? x)? ...

C# DBNull and nullable Types - cleanest form of conversion

I have a DataTable, which has a number of columns. Some of those columns are nullable. DataTable dt; // Value set. DataRow dr; // Value set. // dr["A"] is populated from T-SQL column defined as: int NULL What, then, is the cleanest form of converting from a value in a DataRow, to a nullable variable. Ideally, I would be able t...

Scala: Something like Option (Some, None) but with three states: Some, None, Unknown

I need to return values, and when someone asks for a value, tell them one of three things: Here is the value There is no value We have no information on this value (unknown) case 2 is subtly different than case 3. Example: val radio = car.radioType we know the value: return the radio type, say "pioneer" b. there is no value: re...

Linq grouping by nullable datetime and using this as criteria

I am struggling with a nullable datetime column [DateInsp] in an ASP.NET app which uses SubSonic3, Linq, MS SQL Server 2005. I had this all working when the datetime column [DateInsp] did not allow nulls. A new requirement forced me to set the [DateInsp] column to allow nulls and now I am struggling getting this piece of functionality ...

Is it possible to use operator ?? and throw new Exception() ?

I have a number of methods doing next: var result = command.ExecuteScalar() as Int32?; if(result.HasValue) { return result.Value; } else { throw new Exception(); // just an example, in my code I throws my own one } I wish I could use operator ?? like this: return command.ExecuteScalar() as Int32? ?? throw new Eception(); but ...

Can't Deserialize a Nullable KeyValuePair from JSON with ASP.NET AJAX

The following class does not deserialize (but does serialize) using System.Web.Script.Serialization.JavaScriptSerializer. public class foo { public KeyValuePair<string, string>? bar {get;set;} } The attempt to deserialize results in a System.NullReferenceException when System.Web.Script.Serialization.ObjectConverter.ConvertDictionar...

Is it possible to enforce that a type param is nullable on a class

given a class definition like: public class Test<T> { T _value; public void Test(T value) { _value = value; } public void DoStuff() { if(_value.HasValue) { //stuff } } } I would like to enforce that T is nullable so I can use the class like: //does stuff new T...

Linq: transform memberExpression type to not nullable

the following member expression type can sometimes be NUllable, I m checking that , however I need to convert it to a non nullable type , MemberExpression member = Expression.Property(param, something); var membertype = member.Type; if (membertype.IsGenericType && membertype.GetGenericTypeDefinition() == typeof(Nullable<>)) { // conv...

NHibernate FutureValue and nullable types

I am having trouble using NHibernate's FutureValue method and nullable types. Is this a deficiency of NHibernate, or is the bug on my end? I can't think of a good work around. This is the query I am trying to run: Session.CreateCriteria<MyEntity>() .SetProjection(Projections.Min("NullableDecimalProperty")) .FutureValue<decima...

Should References in Object-Oriented Programming Languages be Non-Nullable by Default?

Null pointers have been described as the "billion dollar mistake". Some languages have reference types which can't be assigned the null value. I wonder if in designing a new object-oriented language whether the default behavior should be for references to prevent being assigned null. A special version of the could then be used to overr...

How can I format a nullable DateTime with ToString()?

How can I convert the nullable DateTime dt2 to a formatted string? DateTime dt = DateTime.Now; Console.WriteLine(dt.ToString("yyyy-MM-dd hh:mm:ss")); //works DateTime? dt2 = DateTime.Now; Console.WriteLine(dt2.ToString("yyyy-MM-dd hh:mm:ss")); //gives following error: no overload to method ToString takes one argument ...

why is null not equal to null false

I was reading this article: http://stackoverflow.com/questions/191640/get-null-null-in-sql And the consensus is that when trying to test equality between two (nullable) sql columns, the right approach is: where ((A=B) OR (A IS NULL AND B IS NULL)) When A and B are NULL, (A=B) still returns FALSE, since NULL is not equal to NULL. That...

testing inequality with columns that can be null

So, I asked a question this morning, which I did not phrase correctly, so I got a lot of responses as to why NULL compared to anything will give NULL/FALSE. My actual question was, what is the time honored fashion in which db guys test inequalities for two columns that can both be NULL. My question is the exact opposite of this questio...

C#: Are nullable types (int?) objects?

I understand from this post that value types in C# are not objects. That is, they do not inherit from System.Object. Assuming my logic holds up to this point, are nullable types, such as int?, objects. Do they inherit from Object? If so, are they subject to all the same rules and constraints as other objects, or are there special rul...

Why aren't F# records allowed to have AllowNullLiteralAttribute?

Is there a compiler implementation reason why records can't have the AllowNullLiteralAttribute attribute or is this a chosen constraint? I do see this constraint force cleaner code sometimes but not always. [<AllowNullLiteralAttribute>] type IBTreeNode = { mutable left : IBTreeNode; mutable right : IBTreeNode; mutable value : int} ...

DateTime/bool and Null values in .Net

Utilizing .Net I am inheriting from an older class that has DateTime and bool parameters as part of its constructor. These values are pulled from a SQL database and in the SQL database these values can be null. When I populate my class and call the base constructor this fails because DateTime and bool cannot have null values in .Net. ...

SubSonic - Serializing Classes with Nullable types and collections?

Using SubSonic v2.x: The first issue is the error discussed here: Server Error in '/......' Application. Cannot serialize member '.....' of type System.Nullable I'm not sure where to place the code in my DAL from this post to get this working. I tried placing it in one of the partial classes I created for a table, but no go. Another ...