-1

I am working on the code to retrieve Exchange email messages by batches of 100.

SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
FindItemsResults<Item> findResults;
ItemView view = new ItemView(100);
int inboxCount = 1;
do
{
    findResults = service.FindItems(WellKnownFolderName.Inbox, sf, view);
    foreach (var item in findResults.Items)
    {
        Console.WriteLine(inboxCount + ". " + item.Subject);
        inboxCount++;
    }

    //error below
    view.Offset = findResults.NextPageOffset;
}
while (findResults.MoreAvailable);

However, I encountered an error on findResults.NextPageOffset;, of the following:

Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)

Any help will be appreciated.

gymcode
  • 4,431
  • 15
  • 72
  • 128

1 Answers1

1

Just use Value, which will give you the int from the nullable int

view.Offset = findResults.NextPageOffset.Value;

However there is a chance it's null, what do you do then? Well, that's up to you.

if(!findResults.NextPageOffset.HasValue)
   throw new InvalidOperationException("Oh dear...");

Further Reading

Nullable types (C# Programming Guide)

Use the Nullable.HasValue and Nullable.Value readonly properties to test for null and retrieve the value, as shown in the following example: if (x.HasValue) y = x.Value;

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Thank you @TheGeneral. I tried your solution, and modified and include the above statement in my code in order. However, before it is able to reach the `if` statement, an error indicating that `Nullable object must have a value`. – gymcode Aug 20 '19 at 03:06
  • @gymcode the check has to go before the call to `Value` the idea is to check if its null and deal with it first before accessing `Value` – TheGeneral Aug 20 '19 at 03:08