[英]C# Linq Check if the content exists in the database
I have a movie database,a listbox on the form show the titles. 我有一个电影数据库,表单上的列表框显示标题。 If the user select a dvd a want to rent it the app must check if it that movie title(foreign key) already in the rent table.
如果用户选择了要租借的DVD,则应用必须检查租借表中是否已存在电影标题(外键)。 If yes write it is in the database,if not write:not in the database.
如果是,则将其写入数据库中;如果未写入,则将其写入数据库中。 If i use the code below i get the error when the movie title is not in the rent table so there is some problem with that NULL.
如果我使用下面的代码,当电影标题不在租金表中时,我会收到错误消息,因此该NULL存在一些问题。 How can i fix this?
我怎样才能解决这个问题?
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");
}
}
You can get count of items which meet the condition like that: 您可以得到满足以下条件的项目数:
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.