received error CS1061 with DateTime?

sblb 1,231 Reputation points
2022-10-25T10:20:31.54+00:00

Hi,

I've a class model : public DateTime DateSolde { get; set; } ;

I would like to transform this class model by ; public DateTime? DateSolde { get; set; } ;

I've also IActionResult in controller.

     public IActionResult MonthlyStats()  
        {  
             int numberecr = context.Developers.Count();  
            int calculN = context.Developers.Count(number => number.DateSolde > number.DateSoldePr && number.Statut == "Close");  
            int open = context.Developers.Count(a => a.DateCrea.Month == DateTime.Today.Month);  
            int close = context.Developers.Count(a => a.DateSolde.Month == DateTime.Today.Month && a.DateSolde.Year == _currentYear);  
  
            double data = Math.Round(((double)calculN) / numberecr, 1);  
  
            var stats = new  
            {  
                NombreECR = calculN,  
                Ratio = data,  
                EcrTotal = numberecr,  
                OpenECR = open,  
                CloseECR = close  
            };  
            return Ok(JsonSerializer.Serialize(stats, new JsonSerializerOptions   
            {   
                PropertyNamingPolicy = null  
            }));  
             
        }  

But when I put DateTime? I received the message

DateTime?' does not contain a definition for 'Year' and no accessible 'Year' extension method accepting a first argument of type 'DateTime?' was found (is a using directive or >assembly reference missing?) WebApp3.Server

It's same things for month.

Why I received this message?

I need to put DateTime? because I use DatePicker and it's necessary to have it.
thanks in advance to your answer

Developer technologies | .NET | Blazor
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. AgaveJoe 30,491 Reputation points
    2022-10-26T11:05:01.85+00:00

    The nullable types do not have a Year or Month member. You must get the value of the nullable type first. Please read the official documentation.

    Example code.

    [HttpGet("TestDateSolde")]  
    public async Task<IActionResult> TestDateSolde()  
    {  
        int _currentYear = 2022;  
        int close = await _context.Developers.CountAsync(a =>   
            (a.DateSolde.HasValue && a.DateSolde.Value.Month == DateTime.Today.Month) &&   
            (a.DateSolde.HasValue && a.DateSolde.Value.Year == _currentYear));  
      
        return Ok(close);  
    }  
    

    Please do not mindlessly copy and paste the sample code like you did above. Make an effort to learn C# syntax.


5 additional answers

Sort by: Most helpful
  1. sblb 1,231 Reputation points
    2022-10-26T12:06:32.277+00:00
     public DateTime? DateSolde { get; set; }  
    
    
    int close = context.Developers.Count(a => (a.DateSolde.HasValue && a.DateSolde.Value.Month == DateTime.Today.Month) && (a.DateSolde.HasValue && a.DateSolde.Value.Year == _currentYear));  
    

    It's OK thanks to everyone!

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.