title: mongo range-based pagination with linq
body: <p>It is possible to do range based pagination with mongo linq or I'm better off with <code>filter</code> route. The following is my case at hand:</p>
  
  <p>I store and generate by Id's as mongo <code>ObjectId</code>s, but treat them as <code>strings</code> in my domain:</p>
  
  <pre><code>    BsonIgnoreIfDefault]
      [BsonRepresentation(BsonType.ObjectId)]
      [BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
      public string Id { get; set; }
  </code></pre>
  
  <p>and I'm trying</p>
  
  <pre><code>var result = await _collection.AsQueryable()
           .Where(m =&gt; m.Id &gt; afterId) // '&gt;' illegal with strings
           .OrderBy(m =&gt; m.Id)
           .ToListAsync(); 
  </code></pre>
  
  <p><code>Error  CS0019  Operator '&gt;' cannot be applied to operands of type 'string' and 'string'</code>.</p>
  
  <p>Another option. My Id's are mongo-generated as <code>ObjectId</code> and I compare them in my filter:</p>
  
  <pre><code>var idFilter = Builders&lt;T&gt;.Filter.Gt(m =&gt; m.Id, afterId);
  result = await _collection.Find(idFilter).ToListAsync();
  </code></pre>