C# 如何在c中使用LINQ从sql数据库中查找重复项

C# 如何在c中使用LINQ从sql数据库中查找重复项,c#,windows,linq,duplicates,C#,Windows,Linq,Duplicates,我有一个使用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.Ex

我有一个使用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;
   } 

根据这一点,若我在textbox leave事件中调用此方法,当数据库中出现重复记录时,我将引发一个错误。现在我希望通过使用linq执行相同的操作。请任何人帮帮我。谢谢你

假设你有一个人口统计列表,并且你想看看有多少符合某个文本值,请尝试以下方法:

 public bool RecordExists(string name)
   {
       List<Demographic> demographics = PopulateList();

       return demographics.Count(d => d.thal_Id == name) > 0;
   } 
public bool RecordExists(string id)
{
    return dc.Demographics.Any(d => d.thal_Id == id);
} 

如果您使用Linq to SQL,将dc作为数据上下文,它应该如下所示:

 public bool RecordExists(string name)
   {
       List<Demographic> demographics = PopulateList();

       return demographics.Count(d => d.thal_Id == name) > 0;
   } 
public bool RecordExists(string id)
{
    return dc.Demographics.Any(d => d.thal_Id == id);
} 

我没有得到大众化的方法。那是什么意思。。我尝试使用公共bool RecordExistsstring name{List demographics=from ins in cstDC.demographics其中ins.name==name选择ins;return demographics.Countd=>d.cst_name==name>1;}在这种情况下,Iam收到错误,因为错误1无法将类型“System.Linq.IQueryable”隐式转换为“System.Collections.Generic.List”。存在显式转换如果缺少一个cast,要使用LINQ,必须有一个列表或可枚举项进行比较。如果您希望完整的代码填充列表,那么有很多选项、ORM、自定义DTO等等。祝您好运。-1因为这个问题与重复项无关。请准确,以便该问题将来对其他人有用。