How to make a LINQ query that returns the amount of people who were born per month?

1

I would like to make a LINQ query that returns the amount of people who were born in the month.

In MySQL I make this query:

SELECT COUNT(*) AS DataNacimento FROM pessoas WHERE MONTH (DataNascimento) = '12';

In LINQ I tried this query, but it does not accept:

var dataNascimento = db.Pessoas.Where(w => w.DataNascimento == "12").Count();
    
asked by anonymous 24.11.2018 / 20:44

1 answer

1

It's simpler than this, but you need to get the month, it did not translate correctly:

var qtdeNascidosDezembro = db.Pessoas.Count(w => w.DataNascimento.Value.Month == 12);

I put Value because the column is nullable (need this?) and then the access is indirect. If it really can be null the ideal would be to treat this and decide what to do when it comes null, otherwise it will give error. If it does not give an error it probably should not allow null because none is. You could deciri using HasValaue or get the value with GetValueOrDefault() .

    
24.11.2018 / 20:49