Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions documentation/documentation/documents/compiled_queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ To the best of our knowledge and testing, you may use any <[linkto:documentation
* `OrderBy/OrderByDescending` etc.
* `Count()`
* `Any()`
* `AsJson()`
* `ToJsonArray()`
* `ToJsonArrayAsync()`

At this point (v0.9), the only limitations are:

Expand Down Expand Up @@ -81,10 +84,40 @@ A sample usage of this type of query is shown below:

## Querying for a single document

Finally, if you are querying for a single document with no transformation, you can use this interface as a convenience:
If you are querying for a single document with no transformation, you can use this interface as a convenience:

<[sample:ICompiledQuery-for-single-doc]>

And an example:

<[sample:FindUserByAllTheThings]>
<[sample:FindUserByAllTheThings]>



## Querying for multiple results as Json

To query for multiple results and have them returned as a Json string, you may run any query on your `IQueryable<T>` (be it ordering or filtering) and then simply finalize the query with `ToJsonArray();` like so:

<[sample:CompiledToJsonArray]>

If you wish to do it asynchronously, you can use the `ToJsonArrayAsync()` method.

A sample usage of this type of query is shown below:

<[sample:FindJsonOrderedUsersByUsername]>

Note that the result has the documents comma separated and wrapped in angle brackets (as per the Json notation).



## Querying for a single document

Finally, if you are querying for a single document as json, you will need to prepend your call to `Single()`, `First()` and so on with a call to `AsJson()`:

<[sample:CompiledAsJson]>

And an example:

<[sample:FindJsonUserByUsername]>

(our ToJson method simply reuturns a string representation of the `User` instance in Json notation)
8 changes: 8 additions & 0 deletions src/Marten.Testing/Linq/compiled_query_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public void a_single_item_compiled_query_SingleOrDefault()
.ShouldBeNull();
}

// SAMPLE: FindJsonUserByUsername
[Fact]
public void a_single_item_compiled_query_AsJson()
{
Expand All @@ -84,7 +85,9 @@ public void a_single_item_compiled_query_AsJson()
user.ShouldNotBeNull();
user.ShouldBe(_user1.ToJson());
}
// ENDSAMPLE

// SAMPLE: FindJsonOrderedUsersByUsername
[Fact]
public void a_sorted_list_compiled_query_AsJson()
{
Expand All @@ -93,6 +96,7 @@ public void a_sorted_list_compiled_query_AsJson()
user.ShouldNotBeNull();
user.ShouldBe($"[{_user1.ToJson()},{_user5.ToJson()}]");
}
// ENDSAMPLE

[Fact]
public void a_filtered_list_compiled_query_AsJson()
Expand Down Expand Up @@ -201,6 +205,7 @@ public Expression<Func<IQueryable<User>, User>> QueryIs()
}
// ENDSAMPLE

// SAMPLE: CompiledAsJson
public class FindJsonUserByUsername : ICompiledQuery<User, string>
{
public string Username { get; set; }
Expand All @@ -212,7 +217,9 @@ public Expression<Func<IQueryable<User>, string>> QueryIs()

}
}
// ENDSAMPLE

// SAMPLE: CompiledToJsonArray
public class FindJsonOrderedUsersByUsername : ICompiledQuery<User, string>
{
public string FirstName { get; set; }
Expand All @@ -225,6 +232,7 @@ public Expression<Func<IQueryable<User>, string>> QueryIs()

}
}
// ENDSAMPLE

public class FindJsonUsersByUsername : ICompiledQuery<User, string>
{
Expand Down