This section documents the supported LINQ query operators.
Any
Without a predicate Any just tests whether the collection has any documents.
var result =
(from c in collection.AsQueryable<C>()
select c)
.Any();
// or
var result =
collection.AsQueryable<C>()
.Any();
Any (with predicate)
With a predicate Any tests whether the collection has any matching documents.
var result =
(from c in collection.AsQueryable<C>()
select c)
.Any(c => c.X == 1);
// or
var result =
collection.AsQueryable<C>()
.Any(c => c.X == 1);
Note that the predicate can be provided either by a where clause or as an argument to Any, so the following are equivalent to the previous query.
var result =
(from c in collection.AsQueryable<C>()
where c.X == 1
select c)
.Any();
// or
var result =
collection.AsQueryable<C>()
.Where(c => c.X == 1)
.Any();
Any with a predicate is not supported after a projection (at least not yet). So the following is not valid:
var result =
collection.AsQueryable<C>()
.Select(c => c.X)
.Any(x => x == 1);
You can usually rewrite such a query by putting an equivalent where clause before the projection (in which case you can drop the projection).
Count
Without a predicate Count just returns the number of documents in the collection.
var result =
(from c in collection.AsQueryable<C>()
select c)
.Count();
// or
var result =
collection.AsQueryable<C>()
.Count();
Count (with predicate)
With a predicate Count returns the number of documents that match the predicate.
var result =
(from c in collection.AsQueryable<C>()
select c)
.Count(c => c.X == 1);
// or
var result =
collection.AsQueryable<C>()
.Count(c => c.X == 1);
Note that the predicate can be provided either by a where clause or as an argument to Count, so the following are equivalent to the previous query.
var result =
(from c in collection.AsQueryable<C>()
where c.X == 1
select c)
.Count();
// or
var result =
collection.AsQueryable<C>()
.Where(c => c.X == 1)
.Count();
Count with a predicate is not supported after a projection (at least not yet). So the following is not valid:
var result =
collection.AsQueryable<C>()
.Select(c => c.X)
.Count(x => x == 1);
You can usually rewrite such a query by putting an equivalent where clause before the projection (in which case you can drop the projection).
Distinct
Distinct returns the unique values of a field or property of the documents in the collection. You use a projection to identify the field or property whose distinct values you want.
var result =
(from c in collection.AsQueryable<C>()
select c.X)
.Distinct();
// or
var result =
collection.AsQueryable<C>()
.Select(c => c.X)
.Distinct();
The projection must select a particular field or property of the document. If the value of that field or property is represented in MongoDB as an array you can also use array indexing to select an item from the array.
var result =
(from c in collection.AsQueryable<C>()
select c.A[i])
.Distinct();
// or
var result =
collection.AsQueryable<C>()
.Select(c => c.A[i])
.Distinct();
ElementAt
ElementAt returns a particular document from a result set. Often you will combine this with a sort order.
var result =
(from c in collection.AsQueryable<C>()
where c.X > 0
orderby c.X
select c)
.ElementAt(index);
// or
var result =
collection.AsQueryable<C>()
.Where(c => c.X > 0)
.OrderBy(c => c.X)
.ElementAt(index);
If the result set has fewer documents than index ElementAt throws an exception.
ElementAtOrDefault
ElementAtOrDefault is just like ElementAt except that if there are fewer documents than index it returns null instead of throwing an exception.
First
First returns the first document from a result set. Often you will combine this with a sort order.
var result =
(from c in collection.AsQueryable<C>()
where c.X > 0
orderby c.X
select c)
.First();
// or
var result =
collection.AsQueryable<C>()
.Where(c => c.X > 0)
.OrderBy(c => c.X)
.First();
If the result set has no documents First throws an exception.
First (with predicate)
This overload of First allows you to provide a predicate as an argument to First. This is an alternative to using a where clause.
var result =
(from c in collection.AsQueryable<C>()
orderby c.X
select c)
.First(c => c.X > 0);
// or
var result =
collection.AsQueryable<C>()
.OrderBy(c => c.X)
.First(c => c.X > 0);
First with a predicate is not supported after a projection (at least not yet). So the following is not valid:
var result =
collection.AsQueryable<C>()
.OrderBy(c => c.X)
.Select(c => c.X)
.Count(x => x > 0);
You can usually rewrite such a query by putting an equivalent where clause before the projection.
If the result set has no documents First with a predicate throws an exception.
FirstOrDefault
FirstOrDefault is just like First except that if there are no matching documents it returns null instead of throwing an exception.
FirstOrDefault (with predicate)
FirstOrDefault with a predicate is just like First with a predicate except that if there are no matching documents it returns null instead of throwing an exception.
Last
Last returns the last document from a result set. Often you will combine this with a sort order.
var result =
(from c in collection.AsQueryable<C>()
where c.X > 0
orderby c.X
select c)
.Last();
// or
var result =
collection.AsQueryable<C>()
.Where(c => c.X > 0)
.OrderBy(c => c.X)
.Last();
If the result set has no documents Last throws an exception.
Last (with predicate)
This overload of Last allows you to provide a predicate as an argument to Last. This is an alternative to using a where clause.
var result =
(from c in collection.AsQueryable<C>()
orderby c.X
select c)
.Last(c => c.X > 0);
// or
var result =
collection.AsQueryable<C>()
.OrderBy(c => c.X)
.Last(c => c.X > 0);
Last with a predicate is not supported after a projection (at least not yet). So the following is not valid:
var result =
collection.AsQueryable<C>()
.OrderBy(c => c.X)
.Select(c => c.X)
.Last(x => x > 0);
You can usually rewrite such a query by putting an equivalent where clause before the projection.
If the result set has no documents Last throws an exception.
LastOrDefault
LastOrDefault is just like Last except that if there are no matching documents it returns null instead of throwing an exception.
LastOrDefault (with predicate)
LastOrDefault with a predicate is just like Last with a predicate except that if there are no matching documents it returns null instead of throwing an exception.
LongCount
LongCount is just like Count except that the return value is a 64-bit integer instead of a 32-bit integer.
LongCount (with predicate)
LongCount with a predicate is just like Count with a predicate except that the return value is a 64-bit integer instead of a 32-bit integer.
Max
Max returns the maximum value of a field or property of the documents in the collection. You use a projection to identify the field or property whose maximum value you want.
var result =
(from c in collection.AsQueryable<C>()
select c.X)
.Max();
// or
var result =
collection.AsQueryable<C>()
.Select(c => c.X)
.Max();
The projection must select a particular field or property of the document. If the value of that field or property is represented in MongoDB as an array you can also use array indexing to select an item from the array.
var result =
(from c in collection.AsQueryable<C>()
select c.A[i])
.Max();
// or
var result =
collection.AsQueryable<C>()
.Select(c => c.A[i])
.Max();
Max (with selector)
This overload of Max lets you select the field or property whose maximum value you want as an argument to Max instead of to Select.
var result =
(from c in collection.AsQueryable<C>()
select c)
.Max(c => c.X);
// or
var result =
collection.AsQueryable<C>()
.Max(c => c.X);
Min
Min returns the minimum value of a field or property of the documents in the collection. You use a projection to identify the field or property whose minimum value you want.
var result =
(from c in collection.AsQueryable<C>()
select c.X)
.Min();
// or
var result =
collection.AsQueryable<C>()
.Select(c => c.X)
.Min();
The projection must select a particular field or property of the document. If the value of that field or property is represented in MongoDB as an array you can also use array indexing to select an item from the array.
var result =
(from c in collection.AsQueryable<C>()
select c.A[i])
.Min();
// or
var result =
collection.AsQueryable<C>()
.Select(c => c.A[i])
.Min();
Min (with selector)
This overload of Min lets you select the field or property whose minimum value you want as an argument to Min instead of to Select.
var result =
(from c in collection.AsQueryable<C>()
select c)
.Min(c => c.X);
// or
var result =
collection.AsQueryable<C>()
.Min(c => c.X);
OrderBy
OrderBy is used to specify an ascending sort order for the result set.
var query =
from c in collection.AsQueryable<C>()
orderby c.X
select c;
// or
var query =
collection.AsQueryable<C>()
.OrderBy(c => c.X);
OrderByDescending
OrderByDescending is used to specify a descending sort order for the result set.
var query =
from c in collection.AsQueryable<C>()
orderby c.X descending
select c;
// or
var query =
collection.AsQueryable<C>()
.OrderByDescending(c => c.X);
Select
Select is used to project a new result type from the matching documents. In the 1.4 version of the C# driver a projection must typically be the last operation (with a few exceptions like Distinct, Max and Min).
var query =
from c in collection.AsQueryable<C>()
select new { c.X, c.Y };
// or
var query =
collection.AsQueryable<C>()
.Select(c => new { c.X, c.Y });
Single
Single returns the first and only document from a result set.
var result =
(from c in collection.AsQueryable<C>()
where c.X > 0
orderby c.X
select c)
.Single();
// or
var result =
collection.AsQueryable<C>()
.Where(c => c.X > 0)
.OrderBy(c => c.X)
.Single();
If the result set has no documents or multiple documents Single throws an exception.
Single (with predicate)
This overload of Single allows you to provide a predicate as an argument to Single . This is an alternative to using a where clause.
var result =
(from c in collection.AsQueryable<C>()
orderby c.X
select c)
.Single(c => c.X > 0);
// or
var result =
collection.AsQueryable<C>()
.OrderBy(c => c.X)
.Single(c => c.X > 0);
Single with a predicate is not supported after a projection (at least not yet). So the following is not valid:
var result =
collection.AsQueryable<C>()
.OrderBy(c => c.X)
.Select(c => c.X)
.Single(x => x > 0);
You can usually rewrite such a query by putting an equivalent where clause before the projection.
If the result set has no documents or multiple documents Single throws an exception.
SingleOrDefault
SingleOrDefault is just like Single except that if there are no matching documents it returns null instead of throwing an exception.
SingleOrDefault (with predicate)
SingleOrDefault with a predicate is just like Single with a predicate except that if there are no matching documents it returns null instead of throwing an exception.
Skip
Use Skip to specify how many documents to skip from the beginning of the result set. Often you will combine Skip with a sort order.
var query =
(from c in collection.AsQueryable<C>()
orderby c.X
select c)
.Skip(100);
// or
var query =
collection.AsQueryable<C>()
.OrderBy(c => c.X)
.Skip(100);
Take
Use Take to specify how many documents to return from the server. When combining Take with Skip often you will also specify a sort order.
var query =
(from c in collection.AsQueryable<C>()
orderby c.X
select c)
.Skip(100)
.Take(100);
// or
var query =
collection.AsQueryable<C>()
.OrderBy(c => c.X)
.Skip(100)
.Take(100);
ThenBy
ThenBy is used to specify an additional ascending sort order for the result set.
var query =
from c in collection.AsQueryable<C>()
orderby c.X, c.Y
select c;
// or
var query =
collection.AsQueryable<C>()
.OrderBy(c => c.X)
.ThenBy(c => c.Y);
ThenByDescending
ThenByDescending is used to specify an additional descending sort order for the result set.
var query =
from c in collection.AsQueryable<C>()
orderby c.X, c.Y descending
select c;
// or
var query =
collection.AsQueryable<C>()
.OrderBy(c => c.X)
.ThenByDescending(c => c.Y);
Where
A where clause is used to specify which documents the query should return. A where clause is a C# expression that maps the query document type to a boolean value. If the expression returns true the document “matches” the query and is included in the result set.
var query =
from c in collection.AsQueryable<C>()
where c.X > 0
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.X > 0);
Sometimes a predicate can be supplied in other places besides a where clause, and it is also possible to have multiple where clauses. When multiple predicates are involved they are combined into a single composite predicate by combining the individual predicates with the && operator.
For example, the following queries are equivalent:
var query =
(from c in collection.AsQueryable<C>()
where c.X > 0
where c.Y > 0)
.First(c.Z > 0);
// or
var query =
(from c in collection.AsQueryable<C>()
where c.X > 0 && c.Y > 0 && c.Z > 0)
.First();