2

The DBContext with DBSet object:

public class TestContext : DbContext
{

       public TestContext() : base("name=TestContext")
    {
        //http://stackoverflow.com/a/6143116/2404470
        Database.SetInitializer<TestContext>(null);
    }

    public System.Data.Entity.DbSet<UI.Models.Test> Tests { get; set; }
}

Using entity framework, I can select all data from database table like this:

db.Tests.ToListAsync()

I need data sorted based on column A and B while preserving async-ness. How can I achieve that?

Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
  • 1
    since you are using async, you can't use `ToListAsync().OrderBy(..)' you can sort it after the `toList` is over again usng the `OrderBy(..)` fucntion – LiranBo Nov 12 '15 at 07:23
  • 1
    Hello @LiranBo - so I can do `db.Conferences.ToList().OrderBy(t=>t.A).OrderBy(t=>t.B)`. How do I preserve the **async-ness**? – Zameer Ansari Nov 12 '15 at 07:29
  • 1
    `db.Conferences.ToList().OrderBy(t=>t.A)` this should work. this `db.Conferences.ToList().OrderBy(t=>t.A).OrderBy(t=>t.B)` will be the same as `db.Conferences.ToList().OrderBy(t=>t.B)` you should try : `db.Conferences.ToList().OrderBy(t=>t.A).ThenBy(t=>t.B)` – LiranBo Nov 12 '15 at 07:36
  • 1
    This was an eye opener for me! :) Still, how do I keep async-ness? I do not see a method returning `awaiter` here – Zameer Ansari Nov 12 '15 at 07:42
  • 1
    well, only partially. { A =1, B =2} { A=2, B =1} ==(first orderby) ==> { A =1, B =2} { A=2, B =1} ==(2nd orderby)==> { A=2, B =1} { A =1, B =2} – LiranBo Nov 12 '15 at 07:42
  • 1
    Hi. `var tests = await db.Tests.ToListAsync(); tests.OrderBy(t=>t.A).OrderBy(t=>t.B)` – Mikhail Vitik Nov 12 '15 at 07:45
  • 1
    maybe you can check [this](https://github.com/Doxense/foundationdb-dotnet-client/issues/44) Think about the logic here, you want to sort something before you have all the elements. I'm not saying it's impossible but it requires some effort – LiranBo Nov 12 '15 at 07:45
  • 1
    Thanks LiranBo. Looks interesting. MikeVitik - the solution doesn't seems to be working. – Zameer Ansari Nov 12 '15 at 07:52

0 Answers0