Entity Framework - Correct way to check for single records before using them

后端 未结 4 1840
情话喂你
情话喂你 2021-02-20 04:11

To get a LIST of records I normally do something along the lines of:

var efCompany = from a in _dbRiv.Company where a.CompanyId == companyFeedInfo.CompanyId sele         


        
4条回答
  •  你的背包
    2021-02-20 04:58

    var efCompany = _dbRiv.Company
                      .SingleOrDefault(a => a.CompanyId == companyFeedInfo.CompanyId);
    
    if (efCompany != null)
    {
        // Use it
    }
    else
    {
        // Report to user, or whatever
    }
    

提交回复
热议问题