{"The cast to value type 'System.Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type."}
I am trying to handle null values in a Queryable List with Generic Type T. I have tried DefaultIfEmpty(0) does not work because cannot convert Type T to int. How do I handle nulls or ints To a list?
public IEnumerable<T> SelectAll<T>(Object TableName)where T:class
{
String TableNameString = Type.GetType(TableName.ToString()).Name;
QueryString = "SELECT" + " " + "*" + " " + "FROM" + " " + TableNameString;
if (Database.Connection.State == ConnectionState.Closed)
Database.Connection.Open();
var a = ((IObjectContextAdapter)this).ObjectContext;
var t = a.ExecuteStoreQuery<T>(QueryString).AsQueryable().DefaultIfEmpty().ToList();
List<T> newList = t;
return newList;
}
Tin your specific case? is itint?SELECT (prop1, prop2, prop3, ISNULL(prop4, 0), ISNULL(prop5, 'NULL') FROM table_name; where prop4 is an integer type and prop5 is a varchar type. Also, you could put the current query in a single string,QueryString = "SELECT * FROM " + TableNameString;