Elfocrash / Cosmonaut

🌐 A supercharged Azure CosmosDB .NET SDK with ORM support
https://cosmonaut.readthedocs.io
MIT License
341 stars 44 forks source link

Orderby and Pagination does not work with sql query. #53

Closed govi2010 closed 5 years ago

govi2010 commented 5 years ago

Hello team , Please change in this file. Cosmonaut-develop\samples\Cosmonaut.Console\Program.cs

Change Following line: var firstPage = await booksStore.Query().WithPagination(1, 10).OrderBy(x => x.Name).ToPagedListAsync(); To var firstPage = await booksStore.Query("select * from c").WithPagination(1, 10).OrderBy(x => x.Name).ToPagedListAsync();

It Gives Error

System.ArgumentException
  HResult=0x80070057
  Message=Expression of type 'System.Linq.IQueryable`1[System.Object]' cannot be used for parameter of type 'System.Linq.IQueryable`1[Cosmonaut.Console.Book]' of method 'System.Linq.IOrderedQueryable`1[Cosmonaut.Console.Book] OrderBy[Book,String](System.Linq.IQueryable`1[Cosmonaut.Console.Book], System.Linq.Expressions.Expression`1[System.Func`2[Cosmonaut.Console.Book,System.String]])'
  Source=System.Linq.Expressions
  StackTrace:
   at System.Dynamic.Utils.ExpressionUtils.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arguments, ParameterInfo pi, String methodParamName, String argumentParamName, Int32 index)
   at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, Expression arg0, Expression arg1)
   at System.Linq.Queryable.OrderBy[TSource,TKey](IQueryable`1 source, Expression`1 keySelector)
   at Cosmonaut.Console.Program.<Main>d__0.MoveNext() in E:\Cosmonaut-develop\samples\Cosmonaut.Console\Program.cs:line 132
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Cosmonaut.Console.Program.<Main>(String[] args)
Elfocrash commented 5 years ago

Hello @govi2010.

This is not valid: var firstPage = await booksStore.Query("select * from c").WithPagination(1, 10).OrderBy(x => x.Name).ToPagedListAsync();

You cannot have a LINQ OrderBy with SQL. It should be var firstPage = await booksStore.Query("select * from c order by c.Name").WithPagination(1, 10).ToPagedListAsync(); instead.

govi2010 commented 5 years ago

@Elfocrash Yes I just did that. But thanks for replying.

Elfocrash commented 5 years ago

Can I close this?