Fork me on GitHub

Asynchronous Querying Edit on GitHub


Linq Operators

Marten adds extension methods to IQueryable for the asynchronous invocation of the common Linq operators:

  • AnyAsync()
  • CountAsync()
  • MinAsync()
  • MaxAsync()
  • AverageAsync()
  • SumAsync()
  • LongCountAsync()
  • FirstAsync()/FirstOrDefaultAsync()
  • SingleAsync()/SingleOrDefaultAsync()
  • ToListAsync()

An example usage of ToListAsync() is shown below:


[Fact]
public async Task use_to_list_async_in_query()
{
    theSession.Store(new User { FirstName = "Hank" });
    theSession.Store(new User { FirstName = "Bill" });
    theSession.Store(new User { FirstName = "Sam" });
    theSession.Store(new User { FirstName = "Tom" });

    await theSession.SaveChangesAsync().ConfigureAwait(false);

    var users = await theSession
        .Query<User>()
        .Where(x => x.FirstName == "Sam")
        .ToListAsync().ConfigureAwait(false);

    users.Single().FirstName.ShouldBe("Sam");
}

Querying by SQL

To query for results with user-supplied SQL, use:

Missing code sample 'using-queryasync' -- Wait for dotnet stdocs to catch up reading samples or CTRL+SHIFT+R to force refresh