2016-12-04 45 views
0

我使用EF 6個對象,有一個帳戶,有些必須設置對象C#EF只傳回有物體

的關係,但不是所有的帳戶有一個設置對象。 如何返回所有具有設置對象的帳戶?

using (IUnitOfWork uw = _uwf.Create()) 
      { 
Dictionary<string, Account> accounts; 
accounts = uw.Account.All.Where(ac=>ac.Setting.IsEnabled).ToDictionary(a => a.Id, a => a); <--- this is the problem, just want the ones that have a Setting otherwise im getting null reference thrown. 
} 

我的倉庫

public class AccountRepository : MarkForDeleteRepository<DomainObjects.Account>, IAccountRepository 
    { 

     public override IEnumerable<Account> All 
     { 
      get 
      { 
       return 
        RepositorySet.Include("Country") 
         .Include("Setting") 
      } 
     } 
+0

我們如何檢查'null'? 'ac.Setting!= null && ac.Setting.IsEnabled','ac.Setting?.IsEnabled == true'等 –

回答

1

添加where條件檢查空

accounts = uw.Account.All.Where(ac=>ac.Setting !=null && ac.Setting.IsEnabled).ToDictionary(a => a.Id, a => a); 
+0

謝謝主席先生,所有排序。 –