Filter Field in List by boolean value in MVC

1.2k views Asked by At

I had help filtering a Foreach loop in my view and I figured I would use that reasoning to now filter a field using the same logic. I need this field to show the last TicketNoteDate that has a PublicFlag == true. The code compiles but it crashes when I try to run it. Here is the code I tried:

@Html.DisplayFor(modelItem => item.TicketNotes.OrderBy(t => t.TicketNoteDate).Where(t => t.PublicFlag == true).Last().TicketNoteDate)

and here is the error I received:

An exception of type 'System.InvalidOperationException' occurred in System.Core.dll but was not handled in user code

If I remove this code:

.Where(t => t.PublicFlag == true)

then it works except for of course the filtering out non public notes.

1

There are 1 answers

0
joaoeduardorf On BEST ANSWER
@Html.DisplayFor(modelItem => item.TicketNotes.Where(t => t.PublicFlag == true).OrderByDescending(t => t.TicketNoteDate).First().TicketNoteDate)