Compare DateTime field in a lambda only part Date

6

In this lambda, the result will almost always be null, since I gave a GetDate () right in the bank and when this happens, I record Date and Time. How do I make this lambda pass only date and compare with DateTime.Now just the date? I tried to use some C # features in lambda and did not accept it.

var resultado = db.T_PDV
                .Where(a => a.DataCadastro == DateTime.Now)
                .Select(i => new { i.CNPJ})
                .ToList();
    
asked by anonymous 02.06.2014 / 20:12

1 answer

6

Use EntityFunctions DbFunctions for such expression: / p>

Difference is that DbFunctions is in System.Data.Entity.DbFunctions and is for version 6+ of the Entity Framework, below use EntityFunctions which is in System.Data.Objects.EntityFunctions

In the specific case, use DbFunctions.TruncateTime or < a href="http://msdn.microsoft.com/en-us/library/system.data.objects.entityfunctions.truncatetime%28v=vs.100%29.aspx"> EntityFunctions.TruncateTime depending on the version of Entity Framework for it to leave only the Data value. Also in the variable its puts the value of Date equal var dta = DateTime.Now.Date; also bringing only Date without time.

var dta = DateTime.Now.Date;    
var resultado = db.T_PDV.Where(a => DbFunctions.TruncateTime(a.DataCadastro) == dta)
                .Select(i => new { i.CNPJ})
                .ToList();
var dta = DateTime.Now.Date;    
var resultado = db.T_PDV.Where(a => EntityFunctions.TruncateTime(a.DataCadastro) == dta)
                    .Select(i => new { i.CNPJ})
                    .ToList();

In this link , you have one similar but using another method. This link server with reference item.

    
02.06.2014 / 20:36