List only future items in C#

Asked

Viewed 63 times

2

I have a database with a list of shows registered with location, date and time, but I need it to return me on the admin screen only the shows that will still happen instead of listing all shows (past and future), if you can help me I thank!

The code is like this:

 public ActionResult Index()
    {
        var model = new EventosViewModel();

        foreach (var evento in _db.Evento.ToList())
        {
            List<DiaEventoAssociadoViewModel> dias = new List<DiaEventoAssociadoViewModel>();
            if (evento.Dias == null)
                evento.Dias = _db.EventoDia.Where(e => e.EventoId == evento.EventoId).ToList();

            foreach (var dia in evento.Dias)
            {
                dias.Add(new DiaEventoAssociadoViewModel
                {
                    Entrada = dia.DataHoraInicio,
                    EventoId = dia.EventoId,
                    Saida = dia.DataHoraFim,
                });
            }

            if (model.Eventos == null)
                model.Eventos = new List<EventoViewModel>();

            model.Eventos.Add(new EventoViewModel(evento)
            {
                Dias = dias
            });
        }

        return View(model);
    }
  • 2

    The answer depends on what would be an "events that have not yet happened" for your business. Try to improve your question, for example: "events that have not yet happened" are events where the Exit field is not filled or is null. With this example we can show how to add this filter to your query if applicable. If possible edit the question and add this information :-)

  • Sorry Renan... I’m new both here at Stack and in the area.. I’m learning a lot still... edited the question! =)

  • Now it’s clearer :-), @Victor Laio has already answered there rs. Now you need to accept the answer if it solves your problem so that the question is not open.

1 answer

1


Well, apparently you want to bring from the bank all records where the event start date is higher than the correct current date?

Just do this:

if (evento.Dias == null)
    evento.Dias = _db.EventoDia.Where(e => e.EventoId == evento.EventoId &&
    DataHoraInicio >= DateTime.Today).ToList();

In this way, it will bring only events where the DataHoraInicio is greater than or equal to the current date.

  • Your questions and answers are very vague friend, unfortunately it is difficult to help you so. What exactly did not work? Gave some error?

  • It did work! Thank you very much Victor!!!

Browser other questions tagged

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