How do you do a QueryFluent<E> OrderBy with multiple fields?

Feb 11, 2015 at 2:59 PM
This is really a Stack Overflow question, but how would one code a QueryFluent<E> OrderBy with multiple fields, like a OrderBy().ThenBy()? I can see under GetCustomersAsync how one could manually order by multiple fields, but how would you code this query into QueryFluent<E>?
var asyncTask = customerService
     .Query(x => x.Country == "USA")
     .Include(x => x
          .Orders
          .Select(y => y.OrderDetails))
     .OrderBy(x => x
          .OrderBy(y => y.CompanyName)
          .ThenBy(z => z.ContactName))
     .SelectAsync();
Coordinator
Feb 12, 2015 at 7:38 PM
Please review integration test CustomerRepositoryTest (https://genericunitofworkandrepositories.codeplex.com/SourceControl/latest#main/Sample/Northwind.Test/IntegrationTests/CustomerRepositoryTests.cs) which examples this.


        [TestMethod]
        public void GetCustomersAsync()
        {
            using (IDataContextAsync context = new NorthwindContext())
            using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(context))
            {
                IRepositoryAsync<Customer> customerRepository = new Repository<Customer>(context, unitOfWork);
                ICustomerService customerService = new CustomerService(customerRepository);

                var asyncTask = customerService
                    .Query(x => x.Country == "USA")
                    .Include(x => x
                        .Orders
                        .Select(y => y.OrderDetails))
                    .OrderBy(x => x
                        .OrderBy(y => y.CompanyName)
                        .ThenBy(z => z.ContactName))
                    .SelectAsync();

                var customers = asyncTask.Result;

                Assert.IsTrue(customers.Count() > 1);
                Assert.IsFalse(customers.Count(x => x.Country == "USA") == 0);
            }
        }
Marked as answer by lelong37 on 2/15/2015 at 8:02 AM
Feb 13, 2015 at 10:48 PM
Edited Feb 13, 2015 at 10:53 PM
Thank you for the response. I saw how the example .Query() returns IQueryFluent<Customer> and by extension .OrderBy() that could take OrderBy().ThenBy().

What I was contemplating was building that logic in the CustomerService itself with named parts so that the MVC Controller didn't have all that business logic, like in this hypothetical example:
customerService
     .GetUSACustomersIncludingOrderDetails()
     .OrderBy(x => x.OrderBy("CompanyName,ContactName"))
     .SelectAsync(x => new CustomerDDL{id = x.id, name=x.CustomerName} );
The OrderBy("CompanyName,ContactName") is Dynamic Linq, which would also be nice to hide away in the service as well .SelectAsync() could do projection on the fly, where the matching names of the View Model CustomerDDL would be the only fields returned from the database. We could even turn that new statement on CustomerDDL to a Map method.

The closest thing I could find was the QueryObjects like CustomerLogisticsQuery FromCountry(string country);