nullable

Are nullable foreign keys allowed in Entity Framework 4?

I have a problem updating a foreign key in an Entity Framework entity. I am using self tracking entities and have an entity with some relations where the foreign key is also present as a property (one of the new features of EF4). The key (an integer) is marked as Nullable and Concurrency Mode fixed. Specifically I have an Alarm entity w...

Is there a more elegant way to add nullable ints?

I need to add numerous variables of type nullable int. I used the null coalescing operator to get it down to one variable per line, but I have a feeling there is a more concise way to do this, e.g. can't I chain these statements together somehow, I've seen that before in other code. using System; namespace TestNullInts { class Prog...

Generic method, unboxing nullable enum

I've made the following extension method ... public static class ObjectExtensions { public static T As<T>(this object pObject, T pDefaultValue) { if (pObject == null || pObject == DBNull.Value) return pDefaultValue; return (T) pObject; } } ... which i use for e.g. reading data like so: string f...

Why do nullable types behave this way

I was having trouble with a nullable type so I wrote the following program to demonstrate the issue I was having and am confused by the results. Here is the program: Module Module1 Public Sub Main() Dim i As Integer? = Nothing Dim j As Integer? = GetNothing() Dim k As Integer? = GetNothingString() If i.HasValue Then ...

Updating nullable boolean fields in the Entity Framework

Changes to nullable bool properties are not saved back to the db in EF4 however other fields which are nullable are updating without any issues. For example, if I execute simple query similar to the following: EmployeeSurvey employeeSurvey = context.EmployeeSurveys.SingleOrDefault(s => s.EmployeeSurveyID == 60); employeeSurvey.Employee...

SQL Server nullable values storage impact

In SQL server, do null values occupy less space than non-null values, or is null represented as a flag indicating that no value is stored (and thus actually requiring MORE space to store null values). The comparisons would not be null values vs. not null values stored in the same column, but null values stored in a nullable column and a...

Make An Integer Null

I have an update function that updates an sql server db table through a dataset. One of the fields in the table is an integer and accepts null values. So when I am populating the update function I need a way to enter a null in when the function wants an integer. I tried to do it this way but _intDLocation = "" throws an exception Dim ...

Object persistence in .NET and SQL Server, with incomplete values

I am building a form in Net framework 4.0 using linq to sql , and had a question about object persistence. I have a case where a user is asked to fill out a very long form several pages in length. Since it is so long, there is a need to SAVE the data midstream. Session level persistence is not an option, since they could SAVE the form...

LINQ Aggregate behavior of nullable types

Hi, Can someone explain what is going on here? Howcome both these things are true? [TestMethod] public void WhatIsGoingOnHere() { List<int?> list = new List<int?> { 1, 2, 3, null, 5, 6 }; Assert.AreEqual(17, list.Sum()); int? singleSum = 1 + 2 + 3 + null + 5 + 6; Assert.IsNull(singleSum); } Specifically, why does th...

Iteration variable of different type than collection?

I have a collection of nullable ints. Why does compiler allows to iteration variable be of type int not int? ? List<int?> nullableInts = new List<int?>{1,2,3,null}; List<int> normalInts = new List<int>(); //Runtime exception when encounter null value //Why not compilation exception? foreach (...

How do I invoke HasValue on a nullable property of an object via reflection?

...

C# Return value of a property which is nullable

Possible Duplicate: Type result with Ternary operator in C# I ran into this scenario, and there doesn't seem to be a natural way to return a nullable int. The code below gives compilation error because the ternary operator doesn't like null. public int? userId { get { int rv; return int.TryParse(userI...

C# 4: Dynamic and Nullable<>

So I've got some code that passes around this anonymous object between methods: var promo = new { Text = promo.Value, StartDate = (startDate == null) ? new Nullable<DateTime>() : new Nullable<DateTime>(DateTime.Parse(startDate.Value)), EndDate = (endDate == null) ? new Nullable<DateTime>() : ...

really funky C# compiler behavior with nullable literals

The C# 4.0 compiler does not complain about this (not even a warning): if(10.0 > null + 1) { } if (myDoubleValue > null) { } And it seems to be always false. What is going on here? Is null automatically converted to Nullable<double> or something? If so why doesn't this work then: double myDoubleValue = null + 1; Also, why ...

SQL Server table relationships practice

What is the recommended way in this situation: Customer ..* <-------------> 0..1 Car So there is a Customer table and a Car table, Customer can have zero or one Car, the Car can be linked to many Customer tables. Should I add a nullable CarID column to Customer or Should I create a Customer_Car_Map table containing CustomerID and Ca...

How is the boxing/unboxing behavior of Nullable<T> possible?

Something just occurred to me earlier today that has got me scratching my head. Any variable of type Nullable<T> can be assigned to null. For instance: int? i = null; At first I couldn't see how this would be possible without somehow defining an implicit conversion from object to Nullable<T>: public static implicit operator Nullable...

Custom Structure that can be assigned Nothing and/or DBNull??

Hi Guys, Please can any one advise me if it is possible to decalre a custom structure that can be assigned Nothing and / or DbNull.Values, and also can be instanciated as Nothing? What I am looking to do is ceate a custom DateTime object that can recieve a DBNull.Value from a database query and also start life as Nothing. IS this possi...

Nullable Types as Property

Is it ok to construct my object entities with this type? public class Patient { public Datetime? AdmissionDate { get; set; } public double? AdmissionFee { get; set } public int? RoomNumber { get; set } } WHat is the drawback if I implemented this in all my entities, because recently, I always encounte...

Can't add null to list of nullables

Possible Duplicate: Adding null to a List<bool?> cast as an IList throwing an exception. List<int?> listONullables = new List<int?>(); IList degenericed = listONullables; // This works fine listONullables.Add(null); // Run time exception: // "The value "" is not of type "System.Nullable`1[System.Int32]" // and cannot be use...

Adding Constraints for Nullable Enum

Hi Everyone, I'm writing some Enum functionality, and have the following: public static T ConvertStringToEnumValue<T>(string valueToConvert, bool isCaseSensitive) { if (typeof(T).BaseType.FullName != "System.Enum" && typeof(T).BaseType.FullName != "System.ValueType") { throw new ArgumentException("Type must be of Enum a...