title: MongoDB Unable to determine the serialization information for lambda
body: <p>I need to build a complex query with the Mongo C# driver. I already have a working query with intellishell.</p>
  
  <pre><code>db.Organizations.aggregate(
  // Pipeline
  [
      // Stage 1
      {
      $match: { 
          "$and" : [
              {
                  "Country.Path" : /^Asia.*/i
              }, 
              {
                  "$or" : [
                      {
                          "OrganizationTypes.Path" : /^Investor.*/i
                      }, 
                      {
                          "OrganizationTypes.Path" : /^Companies.*/i
                      }
                ]
            }, 
            {
                "$or" : [
                    {
                        "Sectors.Path" : /.*Consumer Discretionary\\Recreation Facilities &amp; Svcs.*/i
                    }, 
                    {
                        "Sectors.Path" : /.*Consumer Discretionary\\Consumer Services.*/i
                    }
                ]
            }
        ]
    }
  },
  
  // Stage 2
  {
    $project: { 
        "id" : 1, 
        "Name" : 1, 
        "OrganizationTypes" : 1, 
        "NumberOfSectors" : {
            "$size" : "$Sectors"
        }, 
        "NumberOfOrganisationType" : {
            "$size" : "$OrganizationTypes"
        }, 
        "NumberOfContacts" : {
            "$size" : "$Contacts"
        }
    }
  },
  
  // Stage 3
  {
    $match: 
      { "NumberOfSectors": { $lt: 16 } }
  
  },
  
  // Stage 4
  {
      $unwind: "$OrganizationTypes"
  },
  
  // Stage 5
  {
    $group: { 
        "_id" : "$OrganizationTypes.Path",  
        count: { $sum: 1 }
    }
  }
  ]
   );
  </code></pre>
  
  <p>My c# code is :</p>
  
  <pre><code>        List&lt;FilterDefinition&lt;Organization&gt;&gt; filters = new List&lt;FilterDefinition&lt;Organization&gt;&gt;();
  
          if (filter.CountriesArea != null &amp;&amp; filter.CountriesArea.Count &gt; 0)
          {
              filters.Add(Builders&lt;Organization&gt;.Filter.In("CountryAreas._id", ReferenceTreeHelper.Flatten(filter.Countries).Select(x =&gt; x.Id)));
          }
          if (filter.Countries != null &amp;&amp; filter.Countries.Count &gt; 0)
              filters.Add(Builders&lt;Organization&gt;.Filter.In(x =&gt; x.Country.Id, ReferenceTreeHelper.Flatten(filter.Countries).Select(x =&gt; x.Id)));
  
          if (filter.Sectors != null &amp;&amp; filter.Sectors.Count &gt; 0)
          {
              filters.Add(Builders&lt;Organization&gt;.Filter.In("Sectors._id", ReferenceTreeHelper.Flatten(filter.Sectors).Select(x =&gt; x.Id)));
          }
  
          if (filter.Precision &lt; 4)
          {
              int value = ((filter.Sectors != null) ? filter.Sectors.Count() : 0) + filter.Precision;
              filters.Add(Builders&lt;Organization&gt;.Filter.Lte(x =&gt; x.RootSectorsCount, value));
          }
  
          if (filter.OrganizationTypes != null &amp;&amp; filter.OrganizationTypes.Count &gt; 0)
              filters.Add(Builders&lt;Organization&gt;.Filter.In("OrganizationTypes._id", ReferenceTreeHelper.Flatten(filter.OrganizationTypes).Select(x =&gt; x.Id)));
  
       ProjectionDefinition&lt;Organization&gt; proj = "{ Id : 1, OrganisationName : 1, OrganizationTypes : 1, BusinessSectorsCount : {$size : $Sectors}, OrganisationTypesCount : {$size : $OrganizationTypes}, ContactsCount : {$size : $Contacts} }";
  
       result = await _Repository.Collection.Aggregate()
                  .Match(Builders&lt;Organization&gt;.Filter.And(filters))
                  .Project(proj)
                  .Unwind&lt;SalesCounterDetailsItem&gt;("OrganizationTypes") 
                  .Group(x =&gt; x, g =&gt; new SalesCounterListItem
                  {
                      Id = g.Key.Id,
                      Path = g.Key.OrganizationTypes.Path,
                      Count = g.Key.OrganisationTypesCount
                  })
                  .ToListAsync();
  </code></pre>
  
  <p>I have a problem with the projection. It throws an exception with the message <code>Unable to determine the serialization information for x =&gt; x.Sectors.Count</code> and this is the stackTrace</p>
  
  <pre><code>   at MongoDB.Driver.ExpressionFieldDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
     at MongoDB.Driver.OperatorFilterDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
     at MongoDB.Driver.AndFilterDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
     at MongoDB.Driver.AggregateFluent`2.&lt;&gt;c__DisplayClass13_0.&lt;Match&gt;b__0(IBsonSerializer`1 s, IBsonSerializerRegistry sr)
     at MongoDB.Driver.DelegatedPipelineStageDefinition`2.Render(IBsonSerializer`1 inputSerializer, IBsonSerializerRegistry serializerRegistry)
     at MongoDB.Driver.PipelineStageDefinition`2.MongoDB.Driver.IPipelineStageDefinition.Render(IBsonSerializer inputSerializer, IBsonSerializerRegistry serializerRegistry)
     at MongoDB.Driver.PipelineStagePipelineDefinition`2.Render(IBsonSerializer`1 inputSerializer, IBsonSerializerRegistry serializerRegistry)
    at MongoDB.Driver.MongoCollectionImpl`1.&lt;AggregateAsync&gt;d__20`1.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
    at MongoDB.Driver.IAsyncCursorSourceExtensions.&lt;ToListAsync&gt;d__16`1.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
    at FinQuest.TeamPortal.Controllers.SalesCounterController.&lt;GetOrganisationTypesCounter&gt;d__3.MoveNext() in D:\Projects\FinQuestPlatform\01-Presentation\FinQuest.TeamPortal\Controllers\SalesCounterController.cs:line 83
  </code></pre>
  
  <p>Sorry for the post's length but I think you have all informations for help me (I hope!)
  Thanks in advance</p>
  
  <p>EDIT : add organisation data sample</p>
  
  <pre><code>{ 
  "_id" : BinData(4, "e2Ay8eLeT0yUgZF7x8eErw=="), 
  "Name" : "Toto",  
  "ZipCode" : "1001", 
  "City" : "Islamabad", 
  "Country" : {
      "_id" : "C4826BE4-DCD0-4DAE-A159-A580A81BCFE9", 
      "DisplayName" : "Pakistan", 
      "Parents" : [
          {
              "_id" : "5834EAA5-9B81-497D-A2BB-97BC3CBC79B4", 
              "DisplayName" : "Asia Pacific Rim"
          }, 
          {
              "_id" : "07A45302-9444-459B-B108-7C1EF16B623A", 
              "DisplayName" : "Southern Asia"
          }
      ], 
      "Path" : "Asia Pacific Rim\\Southern Asia\\Pakistan", 
      "PathId" : "5834EAA5-9B81-497D-A2BB-97BC3CBC79B4\\07A45302-9444-459B-B108-7C1EF16B623A\\C4826BE4-DCD0-4DAE-A159-A580A81BCFE9"
  }, 
  "OrganizationTypes" : [
      {
          "_id" : "77E35293-992E-4587-94DF-9B0CDC06DDCE", 
          "DisplayName" : "Transactions Services (TS) department of an accounting firm", 
          "Parents" : [
              {
                  "_id" : "7EE2315D-9B4D-447F-97B5-EA21AC71AC55", 
                  "DisplayName" : "Advisors"
              }, 
              {
                  "_id" : "C948A2E4-852A-4B14-A1ED-65C2B2CB6EC4", 
                  "DisplayName" : "Financial Services providers (FS providers)"
              }
          ], 
          "Path" : "Advisors\\Financial Services providers (FS providers)\\Transactions Services (TS) department of an accounting firm", 
          "PathId" : "7EE2315D-9B4D-447F-97B5-EA21AC71AC55\\C948A2E4-852A-4B14-A1ED-65C2B2CB6EC4\\77E35293-992E-4587-94DF-9B0CDC06DDCE"
      }
  ], 
  "Sectors" : [
      {
          "_id" : "AAFF7B87-76A1-4B5B-95AD-80C65B03AA21", 
          "DisplayName" : "Telecom", 
          "Parents" : [
              {
                  "_id" : "C093170A-7898-430F-8902-FFBD0CD69194", 
                  "DisplayName" : "Communications"
              }
          ], 
          "Path" : "Communications\\Telecom", 
          "PathId" : "C093170A-7898-430F-8902-FFBD0CD69194\\AAFF7B87-76A1-4B5B-95AD-80C65B03AA21"
      }, 
      {
          "_id" : "C57D5E33-6E65-4BE0-801A-F4273F0955F3", 
          "DisplayName" : "Apparel &amp; Textile Products", 
          "Parents" : [
              {
                  "_id" : "50E99257-723C-4780-987A-E71124001EFC", 
                  "DisplayName" : "Consumer Discretionary"
              }
          ], 
          "Path" : "Consumer Discretionary\\Apparel &amp; Textile Products", 
          "PathId" : "50E99257-723C-4780-987A-E71124001EFC\\C57D5E33-6E65-4BE0-801A-F4273F0955F3"
      }, 
      {
          "_id" : "7C05D920-AA39-427F-840E-69C03ADBE55C", 
          "DisplayName" : "Consumer Products", 
          "Parents" : [
              {
                  "_id" : "22A6DD29-66B2-4D22-A2BD-A441CF674926", 
                  "DisplayName" : "Consumer Staples"
              }
          ], 
          "Path" : "Consumer Staples\\Consumer Products", 
          "PathId" : "22A6DD29-66B2-4D22-A2BD-A441CF674926\\7C05D920-AA39-427F-840E-69C03ADBE55C"
      }
  ], 
  "CountryAreas" : [
      {
          "_id" : "C4826BE4-DCD0-4DAE-A159-A580A81BCFE9", 
          "DisplayName" : "Pakistan", 
          "Parents" : [
              {
                  "_id" : "5834EAA5-9B81-497D-A2BB-97BC3CBC79B4", 
                  "DisplayName" : "Asia Pacific Rim"
              }, 
              {
                  "_id" : "07A45302-9444-459B-B108-7C1EF16B623A", 
                  "DisplayName" : "Southern Asia"
              }
          ], 
          "Path" : "Asia Pacific Rim\\Southern Asia\\Pakistan", 
          "PathId" : "5834EAA5-9B81-497D-A2BB-97BC3CBC79B4\\07A45302-9444-459B-B108-7C1EF16B623A\\C4826BE4-DCD0-4DAE-A159-A580A81BCFE9"
      }
  ]
  </code></pre>
  
  <p>}</p>