I have a very simple database. It s a name/value store for various configuration items and I’m trying to simply get them all out and into a list of items.
My LINQ query looks like this:
configurationList = (from c in database.Configurations select new ViewConfigurationSettingModel(c.name, c.value){ IsChanged = (c.name == id) } ).ToArray();
The class ViewConfigurationSettingModel looks like this:
public class ViewConfigurationSettingModel
{
public ViewConfigurationSettingModel(string name, string value)
{
this.Name = name;
this.Value = value;
this.IsChanged = false;
}
public string Name { get; private set; }
public string Value { get; private set; }
public bool IsChanged { get; set; }
}
When I execute the LINQ query I get an exception because ‘IsChanged’ is not a nullable type and cannot contain null. I am stumped how (c.name == id) could result in a null. Both values are strings, but even a comparison with a null should result in a simple true of false.
What going on with LINQ that this all of a sudden results in a null?
A comparison with a null results in a simple true/false in C#, but not in SQL. I suspect that’s the problem here. Assuming
idis non-null, you could try something like:If you can make the
namecolumn non-nullable in the database, that may help too…