0 votes

View last record

I am trying to display the last record of a table to show the picture of that record in a picturebox but the code only works as follows:

                Ficha fco = new Ficha();

            using (Model.GRUDENTEntities db = new Model.GRUDENTEntities())
                var paciente = db.Consulta.FirstOrDefault(x => x.IdPaciente == idp);

                if (paciente == null)

                using (MemoryStream ms = new MemoryStream(paciente.Foto))
                    fco.pbDentagrama.Image = Image.FromStream(ms);



(idp is a variable where I save the patient's id) And so the record is displayed but not the last one, if I try to retrieve the last one that would be LastOrDefault instead of FirstOrDefault I get the following error:

introducir la descripción de la imagen aquí


Leandro Tuttini Points 25288

I understand that the Queries table will have several records for the same customer, in that case you could use

 using (Model.GRUDENTEntities db = new Model.GRUDENTEntities())
     var paciente = db.Consulta.Where(x => x.IdPaciente == idp)
                               .OrderByDescending(x=> x.CampoFecha)

      //resto codigo

as you will see you filter by patient id, sort in descending order so that the highest date is first and then take a single record.

But still a detail, what you are recovering are the consultations, not the patients, that's why I think the variable is named in the mail.


HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: