Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Linq query return one column list from table

How to return handle null values , using which data type

C#
DataTable PreviousQuarter = GroupFive.Tables["PreviousQuarter_108_49_100_51"];
                LastQuarter.Merge(PreviousQuarter);
                
                var AHCT = (from ah in PreviousQuarter.AsEnumerable()
                                  where
                                    ah.Field<int>("ParameterID") == 10 || ah.Field<int>("ParameterID") == 11
                                     || ah.Field<int>("ParameterID") == 12 || ah.Field<int>("ParameterID") == 13
                                  select ah.Field<int>("Tot_Tkts_Closed")).ToList();
                               
                if (AHCT == null)
                {
                    Response = "";
                }


What I have tried:

C#
DataTable PreviousQuarter = GroupFive.Tables["PreviousQuarter_108_49_100_51"];
                LastQuarter.Merge(PreviousQuarter);
                
                var AHCT = (from ah in PreviousQuarter.AsEnumerable()
                                  where
                                    ah.Field<int>("ParameterID") == 10 || ah.Field<int>("ParameterID") == 11
                                     || ah.Field<int>("ParameterID") == 12 || ah.Field<int>("ParameterID") == 13
                                  select ah.Field<int>("Tot_Tkts_Closed")).ToList();
                               
                if (AHCT == null)
                {
                    Response = "";
                }
Posted
Updated 26-Sep-19 6:57am
v2

1 solution

"int" is a struct and cannot be null. Nullable<int> is a class that can be null. The common shortcut is int? which works almost the same as int.

Some differences are that you need to use the Value property to get the non-null value. It returns the default<int> when it's null which is zero (0).

C#
DataTable PreviousQuarter = GroupFive.Tables["PreviousQuarter_108_49_100_51"];
                LastQuarter.Merge(PreviousQuarter);
                
                var AHCT = (from ah in PreviousQuarter.AsEnumerable()
                                  where
                                    ah.Field<int?>("ParameterID").Value == 10 || ah.Field<int?>("ParameterID") == 11
                                     || ah.Field<int?>("ParameterID").Value == 12 || ah.Field<int?>("ParameterID") == 13
                                  select ah.Field<int?>("Tot_Tkts_Closed")).ToList();
                               
                if (AHCT == null)
                {
                    Response = "";
                }
 
Share this answer
 
Comments
Richard Deeming 27-Sep-19 8:49am    
Slight correction: the Value property thrown an exception if the value is null. It's the GetValueOrDefault method which returns 0 if the value is null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900