Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C Linq检查数据库中是否存在该内容_C#_Linq_Null - Fatal编程技术网

C# C Linq检查数据库中是否存在该内容

C# C Linq检查数据库中是否存在该内容,c#,linq,null,C#,Linq,Null,我有一个电影数据库,表格上有一个列表框显示标题。如果用户选择了一张dvd,并且想要租用它,应用程序必须检查是否已经在租用表中找到了电影标题Foreign键。如果是,写入它在数据库中,如果不写入:不在数据库中。如果我使用下面的代码,当电影标题不在租金表中时会出现错误,因此该空值存在一些问题。我怎样才能解决这个问题 private void bt_Letsrent_Click(object sender, EventArgs e) { var c1 = (fr

我有一个电影数据库,表格上有一个列表框显示标题。如果用户选择了一张dvd,并且想要租用它,应用程序必须检查是否已经在租用表中找到了电影标题Foreign键。如果是,写入它在数据库中,如果不写入:不在数据库中。如果我使用下面的代码,当电影标题不在租金表中时会出现错误,因此该空值存在一些问题。我怎样才能解决这个问题

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");
}

@用户3551399不客气。很高兴这有帮助。别忘了将其标记为答案。您可以单击答案旁边的复选标记将其从空心切换为绿色;对于简单的存在性检查,您还可以将db.Rent.Any与相同的谓词一起使用。