2

I'm trying to get a data filtering only by date (truncating the time). The field in database is DateTime.

As reference, I tried this solution that didn't work: 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported where I should use the EntityFunctions that was replaced by DbFunctions, however, It still didn't work.

So I searched more trying to find some code that looks like mine, and then I've found this other link: Using DbFunctions I get error: The specified type member 'Date' is not supported in LINQ to Entities but I'm still getting the same error:

The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

Any suggestion?

My code:

var data = DateTime.Now.Date;
PostVisualizacoes visualizacao = db.PostVisualizacoes.Where(v => v.UsuId == usuario.UsuId && v.PosId == post.PosId && DbFunctions.TruncateTime(v.PosVisData.Date) == data).FirstOrDefault();
Community
  • 1
  • 1
Dan
  • 1,518
  • 5
  • 20
  • 48

1 Answers1

5

You can avoid that exception if you remove the .Date call from your PosVisData property:

var data = DateTime.Now.Date;
PostVisualizacoes visualizacao = db.PostVisualizacoes.Where(v => v.UsuId == usuario.UsuId && v.PosId == post.PosId
                                                              && DbFunctions.TruncateTime(v.PosVisData) == data)
                                                     .FirstOrDefault();

Extract the date part from a DateTime property is the job of TruncateTime static method.

ocuenca
  • 38,548
  • 11
  • 89
  • 102