当数据库为空时显示消息框

Show a message-box when the database is empty

感谢您查看我的问题。基本上,当用户单击一个按钮时,它会根据数据库的内容在消息框中说出以下内容之一:您的假期已获授权,您的假期已被拒绝或您的假期请求已发送。

我想要这样当用户点击按钮并且数据库中没有任何数据因为用户没有发送假期请求时,收到一个消息框说他们还没有预订一个假期。

这是我的代码:

            private void button2_Click_1(object sender, EventArgs e)
    {
        System.Windows.Forms.Form f = System.Windows.Forms.Application.OpenForms["Login"];
        SundownDatabaseEntities6 db = new SundownDatabaseEntities6();
        int id = Convert.ToInt32(((Login)f).idTb.Text);

        var getrecords = db.Holidays.Where(a => a.Id == id).ToList();

            foreach (var record in getrecords)
            {
                if (record.YesOrNo == "yes")
                {
                    MessageBox.Show("The manager has accepted your holiday (From " + record.Datefrom + " - " + record.Dateto + ").");
                }
                else if (record.YesOrNo == "no")
                {
                    MessageBox.Show("The manager has declined your holiday request (" + record.Datefrom + " - " + record.Dateto + ").");
                }
                else if (record.YesOrNo == null)
                {
                    MessageBox.Show("Your holiday request (" + record.Datefrom + " - " + record.Dateto + ") has been sent.\nWaiting for manager to authorise it...");
                }
                else if (record != null)
            {
                MessageBox.Show("You have not booked an holiday.");
            }
            }
        }

问题出在代码的最后一位,'else if(record != null)' 不检查数据库是否为空。有什么建议么?谢谢!

你应该检查 getrecords.Count()

var getrecords = db.Holidays.Where(a => a.Id == id).ToList();
if (getrecords.Count() == 0) 
{
 //  ... here your logic
}

if (!db.Holidays.Any ())

因为如果 getrecords 为空,它不会转到 foreach