I have a table where we store announcements that are displayed on-screen during meetings meeting id, now my employer wants me to add a button that sends announcements to all meeting rooms regardless of room id, I am saving such announcements on the table with a bit value of true or false under name IsAll, so now what I wanna know is, how do I select only the last entry on the table which has the Isall value set to true
ID | roomid | announcement | IsAll
1 | 1 | example | ....
2 | 10 | test | true
in this case, I want to get the announcement with id 2
ID | roomid | announcement | IsAll
1 | 1 | example |false
2 | 10 | test | true
3 | 10 | test | false
in this case, I don't want the SQL to return me anything from the table, with that out of the way I tried
var msg = db.Broadcasts.Where(x => x.IsAll == true).OrderByDescending(x => x.Id).FirstOrDefault();
but it always returns a value that isn't optimal, how can I achieve this? Thanks
CodePudding user response:
So all you need to do is select the last item and check if the IsAll value is true;
public Broadcast GetLastGlobalMessage()
{
var lastBroadcast = db.Broadcasts.OrderByDescending(x => x.Id).FirstOrDefault();
if(lastBroadcast !=null && lastBroadcast.IsAll)
return lastBroadcast;
return null;
}