How to pass null to int column in linq
How to assign null to int column in LINQ. eg.
SQLDBDataContext sqlD = new SQLDBDataContext();
var result = from p in sqlD.loadsRadius(Convert.ToInt32(Request["radius"]), .....
here, if Request ["radius"] is null, throws an error.
I could go through 0, but I need to change in many of the already existing routines.
Thanks in advance. Anil
+1
anil
source
to share
5 answers
You need to cast int to null, for example:
var result = from p in sqlD.loadsRadius(string.IsNullOrEmpty(Request["radius"]) ? (int?)null : int.Parse(Request["radius"]);
+4
Aaron powell
source
to share
If the exception is a FormatException, I assume that the value returned by Request ["radius"] is in fact the string "null". If Request ["radius"] actually returned null, Convert.ToInt32 () will return zero. You can try using with int.TryParse () to avoid the exception.
+2
CodeChef
source
to share
as suggested by Codechef ..
int radius = -1;
int.TryParse(Request["radius"],out radius)
if(radius > 0) // you can remove this clause if you don't want this
{
SQLDBDataContext sqlD = new SQLDBDataContext();
var result = from p in sqlD.loadsRadius(radius)
}
0
Pradeep kumar mishra
source
to share
Look at nullable types: Here.
0
TGnat
source
to share
Can you use ?? operator :
Convert.ToInt32(Request["radius"] ?? "0")
If Request ["radius"] is null, it will return "0" instead.
0
Alexander Prokofyev
source
to share