Hi i have a Linq to Entity Statement that returns a Zipcode.
and I store this return Zip code in my strongly typed Lists
In my List Zipcode Data type is int
when my Liq statement returns Zipcode there are some NULL and Empty values
and I am not able to convert these NULL and Empty values to int
how can i handle this situation.
I tried something like this
ZipCode= Convert.ToInt32(p.ZipCode ?? "0")
any help is greatly appreciated.
thanks
If you are doing this in memory (after results have already been pulled from the database), and to do that in one line, you would write something like
If you are trying to convert the string to an integer inside query at the database, you might have to define a custom defined function that will translate a conversion to the proper cast at the database. See this related question for a possible implementation.
However, I would encourage you not to convert a zip code to an integer. A zip is not a numeric value, despite being composed of numbers (in parts of the world). Treat it as a proper string, which it looks like you are already doing inside
p(and presumably the database). The same premise applies to phone numbers, SSNs, account numbers, etc.