我有一个方法,它将使用sql命令从数据库返回重复记录。代码是:
public bool RecordExists(string name)
{
OleDbCommand cmd = new OleDbCommand("select count(*) from Demographics where thal_Id = '" + txtPtntSmpl.Text + "'", con);
int recordCount = Convert.ToInt32(cmd.ExecuteScalar());
cn.Close();
return recordCount > 0;
}
根据这一点,如果我在文本框中调用此方法,则会在数据库中出现重复记录时引发错误。现在我希望使用linq 进行相同的操作。请有人帮帮我。谢谢你
答案 0 :(得分:1)
假设您有一个人口统计列表,并且您想查看某个文本值匹配的数量,请尝试以下操作:
public bool RecordExists(string name)
{
List<Demographic> demographics = PopulateList();
return demographics.Count(d => d.thal_Id == name) > 0;
}
答案 1 :(得分:1)
如果您使用Linq to SQL,将dc
作为您的数据上下文,它应该看起来像这样:
public bool RecordExists(string id)
{
return dc.Demographics.Any(d => d.thal_Id == id);
}