0

I am strugging with using an anonymous type in Azure Cosmos Linq syntax. This is the SQL I am trying to emit:

        //    @"SELECT        c.Profile.Customer.CompanyName,
        //                    c.Profile.Customer.CustomerID,
        //                    c.CustomFields.Status
        //                    FROM c"
        //    );

My Linq query looks like this but will not compile because the anonymous type doesn't match the type of the IQueryable

            IQueryable<CustomerDocument> query = container.GetItemLinqQueryable<CustomerDocument>()
            .Select(c => new
            {
                c.Profile.Customer.CompanyName,
                c.Profile.Customer.CustomerID,
                c.CustomFields.Status
            }).ToFeedIterator<CustomerDocument>();

If I omit the anonymous type the query runs fine, but it emits SELECT * FROM c which is very slow due to the size of the schema in the container. If I run the SQL statement it is very fast, but obviously I don't want to maintain SQL text in my code if I can avoid it.

Any ideas what I am doing wrong here? Not using Entity Framework in this project.

David Makogon
  • 69,407
  • 21
  • 141
  • 189
  • `var query = ...` and `.ToFeedIterator()` without explicit type parameter. – Svyatoslav Danyliv Jun 23 '22 at 07:58
  • This worked like a charm. I still am struggling to find the way to acquire and console log the sql statement, which would be helpful, but it seems like this did the trick. I could swear I already tried doing what you suggested, maybe I didn't get rid of the type on the ToFeedIterator method, I thought it was required with Cosmos – John Loveland Jun 23 '22 at 14:58
  • This is how C# works with anonymous types. Compiler can deduce which type of generic parameter to use from function parameters. – Svyatoslav Danyliv Jun 23 '22 at 16:12

1 Answers1

0

Any ideas what I am doing wrong here?

Try to use the var or FeedIterator <CustomerDocument> & .ToFeedIterator()

Agree with the @Svyatoslav Danyliv comments.

Below is the sample code from the Microsoft Documentation,

using (FeedIterator<CustomerDocument> query = container.GetItemLinqQueryable<CustomerDocument>().Select(c => new {
        // Required columns here...
         }).ToFeedIterator()) { 
        while (query.HasMoreResults) 
    { 
    foreach(var item in  await query.ReadNextAsync()) 
    { 
        Console.WriteLine(item.ColumnX); 
    } 
     } 
      }

References:

https://github.com/Azure/azure-cosmos-dotnet-v3/issues/893#issuecomment-559760853

https://stackoverflow.com/a/57171695/18935775

RajkumarPalnati
  • 541
  • 2
  • 6