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. AgaveJoe 30,491 Reputation points
    2022-10-25T10:36:51.407+00:00

    It's a compiler error due to incorrect syntax! Like all your posts, make an effort read the openly published (nullable type) documentation rather than guessing the C# syntax.

    DateTime? date = DateTime.Now;  
    int year = date.Value.Year;  
    

    Nullable value types (C# reference)


  2. Viorel 125.8K Reputation points
    2022-10-25T13:21:08.277+00:00

    I think that you should use a.DateSolde?.Year == _currentYear in your particular case. It will be false if DateSolde is null and _currentYear is a number. (The null rows will be excluded).

    But if you do not expect null dates, then revert to 'public DateTime DateSolde' and find a different solution for the problem of DatePicker, maybe DateSolde = DatePicker.SelectedDate.Value.


  3. AgaveJoe 30,491 Reputation points
    2022-10-25T15:08:13.767+00:00

    For the second time, you specifically defined a nullable DateTime type (DateTime?). The DateTime? type does not have a definition for Year or Month which is clearly stated in the compiler error message.

    254012-capture.png

    The value of the nullable type is needed. See my first post.

    Something like the pattern below might work but you have to understand that the community has no idea how your design is supposed to work or why you decided to change from a DateTime to the nullable type; DateTime?.

    DateTime? date = DateTime.Now;  
    int year = date.HasValue ? date.Value.Year : 0;  
    

  4. Bruce (SqlWork.com) 82,146 Reputation points Volunteer Moderator
    2022-10-25T16:22:51.15+00:00

    if you make a.DateSolde nullable, then you need to the same logic:

    a.DateSolde.HasValue && a.DateSolde.Value.Month == DateTime.Today.Month

    or

    a.DateSolde?.Month == DateTime.Today.Month


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.