Uploaded image for project: 'Couchbase Documentation'
  1. Couchbase Documentation
  2. DOC-7117

DotNet SDK3 - add documentation of query results for C# 7 and below

    XMLWordPrintable

Details

    • Improvement
    • Resolution: Won't Fix
    • Major
    • None
    • None
    • sdk
    • 1

    Description

      A lot of software still being developed with C# lang level less than 8.

      for that reason, I think It would be beneficial to have also the code of how to get n1ql query results in C# 7.

      in C# 8 you would do something like that

       

      var cluster = await _fixture.GetCluster().ConfigureAwait(false);
      var result = await cluster.QueryAsync<dynamic>("SELECT default.* FROM `default` WHERE type=$name;",
       parameter =>
       {
       parameter.Parameter("name", "person");
       }).ConfigureAwait(false);
      await foreach (var o in result.ConfigureAwait(false))
       {
       _testOutputHelper.WriteLine(JsonConvert.SerializeObject(o, Formatting.None));
       }
      

      while in earlier versions:

       

      public async Task Test_QueryWithAsyncStreamCSharp7()
       {
       var cluster = await _fixture.GetCluster().ConfigureAwait(false);
      var result = await cluster.QueryAsync<dynamic>("SELECT default.* FROM `default` WHERE type=$name;",
       parameter =>
       {
       parameter.Parameter("name", "person");
       }).ConfigureAwait(false);
      // Async streaming approach in C# 7
       var enumerator = result.GetAsyncEnumerator();
       try
       {
       while (await enumerator.MoveNextAsync().ConfigureAwait(false))
       {
       _testOutputHelper.WriteLine(JsonConvert.SerializeObject(enumerator.Current, Formatting.None));
       }
       }
       finally
       {
       await enumerator.DisposeAsync().ConfigureAwait(false);
       }
      result.Dispose();
      

       

      Source:

      https://github.com/couchbase/couchbase-net-client/blob/f6f6a89e257adce1251b2b9a4e7836c5932ed539/tests/Couchbase.IntegrationTests/Services/Query/QueryTests.cs#L65

       

       

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              richard.smedley Richard Smedley
              roi.katz Roi Katz
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes

                  PagerDuty