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:
var users =
await
session.QueryAsync<User>(
"select data from mt_doc_user where data ->> 'FirstName' = 'Jeremy'")
.ConfigureAwait(false);
var user = users.Single();