I have this query:
int cid = (from c in dc.Cs
where c.id == cid
select c.clientid).FirstOrDefault();
return cid;
where c.clientid is nullable. But I receive this error:
Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)
Best Solution
Well,
int?
can benull
whileint
can't. That's why the compiler complains: what should it assign tocid
when the query returnsnull
. You can try providing some default value in case ofnull
:which means return
0
in case ofnull
(please, notice that you have two possibilities: 1. 1st itemc.clientid
isnull
; 2.dc.Cs
when filtered is empty) andint?.Value
otherwise