[英]C# Linq Check if the content exists in the database
我有一個電影數據庫,表單上的列表框顯示標題。 如果用戶選擇了要租借的DVD,則應用必須檢查租借表中是否已存在電影標題(外鍵)。 如果是,則將其寫入數據庫中;如果未寫入,則將其寫入數據庫中。 如果我使用下面的代碼,當電影標題不在租金表中時,我會收到錯誤消息,因此該NULL存在一些問題。 我怎樣才能解決這個問題?
private void bt_Letsrent_Click(object sender, EventArgs e)
{
var c1 = (from s in db.Rent where s.Movietitle == (string)listbox1.SelectedValue select s).FirstOrDefault();
if (c1.Movietitle==null)
{
MessageBox.Show("Not in the database");
}
else
{
MessageBox.Show("In the database");
}
}
您可以得到滿足以下條件的項目數:
int count= db.Rent.Count(s => s.Movietitle == (string)listbox1.SelectedValue);
if (count>0)
{
MessageBox.Show("In the database");
}
else
{
MessageBox.Show("Not in the database");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.