Consult by Date

Asked

Viewed 221 times

0

Well I am using the EF with the following code to query it brings the query by name and type but by date not, since the time is not in my filter with the time 00:00:00 it brings correctly the time is not part of the search, but is part of the business rule.

public PaginationResult<Tabela> GetAll(ISpecification<Tabela> specification, Pagination pagination = null, Sorting sorting = null)
{
    var query = Tabela.Where(specification.SatisfiedBy());
    var result = new PaginationResult<Schedule>();

    if (sorting != null)
    {
        query = query.OrderBy(sorting.ToString());
    }
    result.TotalItems = query.Count();
    if (pagination != null)
    {
        query = query.Skip(pagination.CurrentPage * pagination.ItemsPerPage).Take(pagination.ItemsPerPage);
    }

    result.Items = query.ToList();
    return result;
}

The specification

specification = 
          specification.And(Specification<tabela>.Create(e => e.data.Equals(filter.data)));
  • 1

    Is there any way you can edit the specification of your problem? I couldn’t understand anything.

  • Well I want to ignore the hour in the consultation

  • What kind of Data ? field edit your question and post a class of your model with the example of your method call.

  • 1

    Good I’m redoing can leave

1 answer

2


If a hora não faz parte da pesquisa, I believe you can only take the date portion by adding the .Date on the date of the objects when creating Specification:

specification = specification.And(Specification<tabela>.Create(e => DbFunctions.TruncateTime(e.data) <= filter.data.Date));
  • brings this error: There is no support for the 'Date' member of the type specified in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

  • Try to remove the .Date of the Line expression: specification = specification.And(Specification<tabela>.Create(e => e.data.Equals(filter.data.Date))); I believe this solves.

  • Well he does not from error, but brings nothing.

  • So I think the solution would be to not use the equals and make the comparison as follows: specification = specification.And(Specification<tabela>.Create(e => e.data.Date == filter.data.Date));

  • The same mistake happened

  • If you are using from EF 6, you can use the function DbFunctions.TruncateTime to remove date portion: specification = specification.And(Specification<tabela>.Create(e => DbFunctions.TruncateTime(e.data) == filter.data.Date)); If you are using an EF version less than 6, use the function EntityFunctions.TruncateTime for this: specification = specification.And(Specification<tabela>.Create(e => EntityFunctions.TruncateTime(e.data) == filter.data.Date));

  • Well happened another mistake, but let me change the whole logic of the application even, thank you

  • 1

    I used <= and it worked thanks

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.