Click here to Skip to main content
15,566,496 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All

I have this query

aquery = (from p in ctx.rap_t009_i.Where(x => (x.d_prog <= dDat))
          join r in ctx.t011 on p.id_prog equals r.id_prog
          join s in ctx.t005 on r.id_pers equals s.id_pers
          select new TAP_UTILITY.cl_PRIVACY

              c_nom = s.c_nom,
              c_cog = s.c_cog,
              c_cod = s.c_cod,
       ---->  s_date_birth =  s.d_date_birth.ToString("dd/MM/yyyy"), <-----
              c_email = s.c_email

But this query doesn't work. I receive this error message:

Error	CS1501	No overload for method 'ToString' takes 1 arguments	

What I have tried:

I try with this code, but doesn't work.

s_date_birth = s.d_date_birth.HasValue ? s.d_date_birth.ToString("dd/MM/yyyy") : string.Empty,

How can I resolve ?

Thanks to everyone who will help me
Updated 26-Nov-21 7:02am
Richard MacCutchan 26-Nov-21 12:17pm    
Why not just leave that field as it is? You do not need it formatted to a string until you need to display it somewhere.

1 solution

Based on your code I think it's safe to assume that s.d_date_birth is a DateTime? value. This is a Nullable<DateTime> which doesn't have a ToString() method which accepts a format.

You almost had it right in your attempted code, you could try doing:

s_date_birth = s.d_date_birth?.ToString("dd/MM/yyyy") ?? string.Empty,

That will loosely compile to:
s_date_birth = s.d_date_birth.HasValue ? s.d_date_birth.Value.ToString("dd/MM/yyyy") : string.Empty,

You have to access the Value property of a nullable type to get the underlying value.
Share this answer
BillWoodruff 27-Nov-21 9:18am    
antobaro 28-Nov-21 12:39pm    
Hi Cris. Great !! ;-) Thank you very much

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