Linq查询以检查两个值

时间:2016-04-24 13:12:38

标签: asp.net-mvc entity-framework linq

我是新手使用linq查询数据库,只是想知道我的查询是否有任何缺陷,因为它没有按我的意愿行事。我只想知道数据库中是否有任何与两个数字匹配的项目。

如果是,则不应该向数据库中添加任何内容,但它似乎继续添加新内容,尽管我检查一个项目是否已经用这个数字加了!我做错了什么,如何改进我的查询?

        if(db.Member.Any(x => x.ID == c && x.CountryID == d))
        {
            Do something if there is a match and the member already excist in DB...
        } else
        {
          Write new Member to DB....  
        }

1 个答案:

答案 0 :(得分:1)

请尝试这个版本。

var memberObject=db.Member.Where(x => x.ID == c && x.CountryID == d).FirstOrDefault();

if (memberObject==null){
  //Write new Member to DB....  
}else{
  //Do something if there is a match and the member already excist in DB...
}
相关问题