-1

After doing a reasearch i found that document db api supports grouping, so i have implemented using linq,

How can i see the results got from the dataase?

static void Main(string[] args)
        {
            Program p = new Program();
            var result =  p.GetStartedDemo().Wait();
        }

        private async Task GetStartedDemo()
        {
            var client = new DocumentClient(new Uri(EndpointUri), PrimaryKey);
            var documentUri = UriFactory.CreateDocumentCollectionUri("testdb", "retrieve");
            var result = client.CreateDocumentQuery<Login>(documentUri)
                  .Where(i => i.logevent == "success" && i._ts > 1517405472 && i._ts <= 1518010272)
                  .GroupBy(t => t._ts);

            return result;
        }

ERROR:

var result = p.GetStartedDemo().Wait();

Cannot assign implicitly typed code variable. what is the issue?

  • 1
    Task means that the method doesn't return a value it just returns a task that tells you when it's done. Task is what it should return where T is the type of the object you're returning – Dave Feb 07 '18 at 19:06
  • i want to see the output of the returned results. how to do that –  Feb 07 '18 at 19:07
  • i am stuck with this, atleast guide me –  Feb 07 '18 at 19:10
  • 1
    I don't know the details of cosmosdb, I haven't used it. I'm on my phone ATM so if no one helps before my laptop finishes updating ill take another look. Sounds like you need do a bit of learning on Tasks and async programming – Dave Feb 07 '18 at 19:14
  • @Dave please, i am stuck –  Feb 07 '18 at 19:20
  • what about : `var result = p.GetStartedDemo().Result;` – Ehsan Sajjad Feb 07 '18 at 19:28
  • The docs say you are using an obsolete API. See the updated async docs: https://learn.microsoft.com/en-us/azure/cosmos-db/sql-api-dotnet-samples#document-examples – Crowcoder Feb 07 '18 at 19:28

1 Answers1

2

You are executing the "Wait" method of a task object and trying to assign its return to the "result" variable. If you check the Task.Wait() method signature you'll see it has no return:

public void Wait();

I'm your example there is no need for your GetStartedDemo to return a Task and to be "async", you are not doing any asynchronous operation inside it. You can simply return the for you want fit the caller method.

If you want to learn about CosmosDB I suggest checking the Microsoft documentation about it

For your code to work you could simply change the return type of your method to actually match what you are returning:

// Considering that Login._t is of type "long":
private IEnumerable<IGrouping<long, Login>> GetStartedDemo()
{ /* ... */ }    

Then, in your Main method, you can print the items grouped by Login._t :

    static void Main(string[] args)
    {
        Program p = new Program();
        var result =  p.GetStartedDemo();
        foreach (var group in result)
        {
            Console.WriteLine("Group: " + group.Key);

            foreach (Login login in group)
               Console.WriteLine("    " + login.SomeProperty); // print something from the login object.
        }
    }
IPValverde
  • 2,019
  • 2
  • 21
  • 38
  • there is no namespace Grouping –  Feb 08 '18 at 01:38
  • Sorry, fixed it, the type is the interface IGrouping: https://msdn.microsoft.com/en-us/library/bb344977(v=vs.110).aspx – IPValverde Feb 08 '18 at 07:10
  • thanks that worked i was able to fix it. can you lok this question https://stackoverflow.com/questions/48676519/query-expression-is-invalid-cosmosdb/48678821#48678821 –  Feb 08 '18 at 07:36
  • i need to add a count for the date –  Feb 08 '18 at 07:36
  • What do you mean by that? Do you wanna count the amount of Login records with the same date? – IPValverde Feb 08 '18 at 14:21