如何通过在c#中选择数据集命令来检查记录是否已经存在?

时间:2016-10-31 06:09:56

标签: c# asp.net c#-4.0

我正在尝试调整以下代码,以检查数据库中是否存在记录。

    private bool IsAlreadyExist(string LicenseType, int JurisdictionId)
    {
        LicenceBL lbl = new LicenceBL(0);
        DataSet lds = new DataSet();
        lbl.FetchForEdit(lds, AgentId, BrokerId);
        if (lds.Tables[0].Select('LicenseType='+LicenseType+'and Jurisdiction='+JurisdictionId).Count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

它不起作用。它抛出错误......请帮助我!!!

2 个答案:

答案 0 :(得分:0)

Select返回行数组,Count来自Linq。因此,您需要将其称为方法Count()而不是属性。

if (lds.Tables[0].Select('LicenseType='+LicenseType+
      'and Jurisdiction='+JurisdictionId).Count() > 0)
{

同样代替if/else,以下陈述就足够了:

return lds.Tables[0].Select('LicenseType='+LicenseType+
      'and Jurisdiction='+JurisdictionId).Count() > 0;

答案 1 :(得分:0)

奈达,试试这个:

<强>(1)

if (lds.Tables[0].Select('LicenseType='+LicenseType+'and Jurisdiction='+JurisdictionId).Length > 0) {

}

<强>(2)

<强> LINQ

int count = lds.Tables[0].AsEnumerable()
.Where(x => x["LicenseType"] == YourValue && y => y["Jurisdiction"]==YourValue).ToList().Count;

if (count >0) { // do your stuff}