How to convert C# Linq to cosmos query

Juhi Saxena 41 Reputation points

Hi Everyone!

We have below C# LINQ query to group from a cosmos IQueryable container and then order by some attributes.

var unitPrices = unitPriceContainer.ToList()
.GroupBy(u => new { u.SupplierId, u.Sku, u.SupplierPackSize })
.Select(s => s.OrderByDescending(o => o.CreatedDate).ThenByDescending(t => t.EffectiveFrom).First())

We want to convert this to Cosmos query so that it can be accessible using FeedIterator.

Is there any workaround to have same query converted or just group by. So that, order by can further be queried over the output of group by. Please suggest.

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,469 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,391 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 4,221 Reputation points

    Hi @Juhi Saxena

    The Azure Cosmos DB query provider performs a best effort mapping from a LINQ query into a Cosmos DB SQL query. If you want to get the SQL query that is translated from LINQ, use the ToString() method on the generated IQueryableobject.

    There is a guide on how convert LINQ to SQL translation in the below Microsoft link.

    Hope this will be helpful and serve as a library for you.

    Hope to read from you.


    0 comments No comments