title: The cast to value type System.Boolean failed because the materialized value is null, result type's generic parameter must use a nullable type
body: <p>This code was working before but now I've got this error: <strong>The cast to value type 'System.Boolean' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.</strong></p>
  
  <pre><code>public async Task&lt;ActionResult&gt; BankDepositVoucher(BankDepositVoucherSearchViewModel search, int? PageNo)
          {
              var model = new BankDepositVoucherListViewModel
              {
                  Search = search ?? new BankDepositVoucherSearchViewModel()
              };
              if (search != null)
              {
                  search.StartDate = search.StartDate.ToStartOfDay();
                  search.EndDate = search.EndDate.ToEndOfDay();
              }
              try
              {
                  var Vouchers = DbManager.Invoices.Include(x =&gt; x.BankDepositVoucher)
                                               .Where(x =&gt; x.Type == InvoiceType.BankDepositVoucher
                                               &amp;&amp; (x.VoucherNumber == search.VoucherNo || search.VoucherNo == null)
                                               &amp;&amp; (x.BankDepositVoucher.SlipNo.Contains(search.SlipNo) || search.SlipNo == null)
                                               &amp;&amp; (x.BankDepositVoucher.ChequeNo.Contains(search.ChequeNo) || search.ChequeNo == null)
                                               &amp;&amp; (x.BankDepositVoucher.Bank.AccountName.Contains(search.BankDetails)
                                               || search.BankDetails == null)
                                               &amp;&amp; (x.BankDepositVoucher.AccountName.Contains(search.AccountName) || search.AccountName == null)
                                               &amp;&amp; (x.BankDepositVoucher.Narration.Contains(search.Narration) || search.Narration == null)
                                               &amp;&amp; (x.TotalAmount == search.Amount || search.Amount == null)
                                               &amp;&amp; (x.Date &gt;= search.StartDate || search.StartDate == null)
                                                &amp;&amp; (x.Date &lt;= search.EndDate || search.EndDate == null));
                  //model.Pager = new Pager(await Vouchers.CountAsync(), PageNo, 10);
                  model.Vouchers = await Vouchers.OrderByDescending(x =&gt; x.VoucherNumber)
                                      //.Skip((model.Pager.CurrentPage - 1) * model.Pager.PageSize)
                                      //.Take(model.Pager.PageSize)
                                      .Select(x =&gt; new BankDepositVoucherBaseViewModel
                                      {
                                          Id = x.Id,
                                          VoucherNumber = x.VoucherNumber,
                                          AccountName = x.BankDepositVoucher.AccountName,
                                          BankAccountName = x.BankDepositVoucher.Bank.AccountName,
                                          Date = x.Date,
                                          ChequeNo = x.BankDepositVoucher.ChequeNo,
                                          Narration = x.BankDepositVoucher.Narration,
                                          SlipNo = x.BankDepositVoucher.SlipNo,
                                          TotalAmount = x.TotalAmount,
                                          IsCleared = x.BankDepositVoucher.IsCleared
                                      }).ToListAsync();
              }
              catch (Exception ex)
              {
                  Console.WriteLine("", ex.Message);
  
              }
              return PartialView(model);
          }
  </code></pre>
  
  <p>This is the part throwing above mentioned exception</p>
  
  <pre><code>model.Vouchers = await Vouchers.OrderByDescending(x =&gt; x.VoucherNumber)
                                      //.Skip((model.Pager.CurrentPage - 1) * model.Pager.PageSize)
                                      //.Take(model.Pager.PageSize)
                                      .Select(x =&gt; new BankDepositVoucherBaseViewModel
                                      {
                                          Id = x.Id,
                                          VoucherNumber = x.VoucherNumber,
                                          AccountName = x.BankDepositVoucher.AccountName,
                                          BankAccountName = x.BankDepositVoucher.Bank.AccountName,
                                          Date = x.Date,
                                          ChequeNo = x.BankDepositVoucher.ChequeNo,
                                          Narration = x.BankDepositVoucher.Narration,
                                          SlipNo = x.BankDepositVoucher.SlipNo,
                                          TotalAmount = x.TotalAmount,
                                          IsCleared = x.BankDepositVoucher.IsCleared
                                      }).ToListAsync();
  </code></pre>