title: mongodb c# sort by field in a list of entries
body: <p>I have a collection like this (I removed fields that are not related to the question)</p>
  
  <pre><code>{
     _id:ObjectId('5dd7d946cd9c645f1cdc21ef'),
     Versions: [
        {
             "Barcode" : "200830001128132700636"
        },
         {
             "Barcode" : "200830001128132700637"
        }
     ]
  },
  {
     _id:ObjectId('5dd7d946cd9c645f1cdc21eg'),
     Versions: [
        {
             "Barcode" : "200830001128132700638"
        },
         {
             "Barcode" : "200830001128132700639"
        }
     ]
  }
  </code></pre>
  
  <p>I need to find the greatest (max) barcode in the whole collection.</p>
  
  <p>I tried with a code like this:</p>
  
  <pre><code>var options = new FindOptions&lt;Document&gt;
          {
              Limit = 1,
              Sort = Builders&lt;Document&gt;.Sort.Descending(d =&gt; d.Versions.Select(v =&gt; v.BarCode).Aggregate((v1, v2) =&gt; string.Compare(v1, v2) &gt; 0 ? v1 : v2))
          };
  using var results = await _context.DocumentiItems.FindAsync(FilterDefinition&lt;Document&gt;.Empty, options);
  </code></pre>
  
  <p>But I get ArgumentNullException, I think it's unable to traslate the expression with the aggregate.</p>
  
  <p>Can you suggest me a better approach?, if possible I want to avoid the use of BSON strings and use only labmda expressions.</p>
  
  <p>The type of is DocumentiItems is <code>IMongoCollection&lt;Document&gt;</code></p>