2
votes

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();
1
i think truncating you have to make outside , not in EnfwRahul Chaudhari

1 Answers

5
votes

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.