C# Driver LINQ Tutorial
1.介绍
该教程涵盖了1.8版本的C#驱动中的LINQ查询。你可能已经阅读最新的C# Driver Tutorial。
2.快速开始
首先,给程序添加下面的using声明
using MongoDB.Driver.Linq;
然后,像往常一样,获取一个collection的引用变量:
var collection = database.GetCollection<TDocument>("collectionname");
写LINQ查询背后的基本思路,是从一个集合变量开始,用AsQueryable<TDocument>()方法开始LINQ查询。之后,是标准的LINQ。
var query =
from e in collection.AsQueryable<Employee>()
where e.FirstName == "John"
select e; foreach (var employee in query)
{
// process employees named "John"
}
你也可以使用lambda语法,写查询。上面的例子使用lambda语法会携程这样:
var query =
collection.AsQueryable<Employee>()
.Where(e => e.FirstName == "John");
C#编译器在内部将所有查询语法写的查询,翻译成lambda语法,所以选择任何一种风格,没有任何性能优势或惩罚。你可以混合并匹配风格,这样在查询操作不被查询语法支持时,非常有用。
本教程所有的例子,都是用两种语法写。
只有LINQ查询,可以被翻译成等价的MongoDB插叙。如果你写一个LINQ查询,不能被翻译,你会得到一个运行时异常和错误消息,来表明哪部分查询不被支持。
3.支持的LINQ查询操作
Any
没有条件的Any,仅仅测试该集合是否有任何文档。
var result =
(from c in collection.AsQueryable<C>()
select c)
.Any();
// or
var result =
collection.AsQueryable<C>()
.Any();
Any(判断)
使用判断的Any,测试集合中是否有任何匹配的文档。
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);
使用判断的Any,不能在一个项目后面。所以下面不行:
var result =
collection.AsQueryable<C>()
.Select(c => c.X)
.Any(x => x == 1);
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)
.First(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 nullinstead 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 nullinstead 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);OfType
The OfType operator will insert a discriminator into the query in order to be more specific about choosing the correct documents.
var result =
(from c in collection.AsQueryable<C>().OfType<D>()
select c)
// or
var result =
collection.AsQueryable<C>()
.OfType<D>();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. A projection must typically be the last operation (with a few exceptions like Distinct, Max and Min).
WARNING
Select does not result in fewer fields being returned from the server. The entire document is pulled back and passed to the native Select method. Therefore, the projection is performed client side.
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 returnsnull 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 withSkip 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();
Supported where clauses
This section documents the supported where clauses.
As mentioned earlier, not all C# expressions are supported as a where clause. You can use this documentation as a guide to what is supported, or you can just try an expression and see if it works (a runtime exception is thrown if the where clause is not supported).
Where clauses are typically introduced using the Where query operator, but the same expressions are supported wherever a predicate is called for. In some cases multiple where clauses and predicates will be combined, in which case they are combined with the && operator.
NOTE
The 1.4 version of the C# driver requires that all where clauses that compare a field or property against a value have the constant on the right hand side. This restriction will be lifted in the next release.
&& (And operator)
Sub-expressions can be combined with the && operator to test whether all of them are true.
var query =
from c in collection.AsQueryable<C>()
where c.X > 0 && c.Y > 0
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.X > 0 && c.Y > 0);This is translated to the following MongoDB query:
{ X : { $gt : 0 }, Y : { $gt : 0 } }In some cases the And query can’t be flattened as shown, and the $and operator will be used. The following example matches documents where X is both a multiple of 2 and a multiple of 3:
var query =
from c in collection.AsQueryable<C>()
where (c.X % 2 == 0) && (c.X % 3 == 0)
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => (c.X % 2 == 0) && (c.X % 3 == 0));This is translated to the following MongoDB query using $and:
{ $and : [{ X : { $mod : [2, 0] } }, { X : { $mod : [3, 0] } }] }Any
This method is used to test whether an array field or property contains any items.
var query =
from c in collection.AsQueryable<C>()
where c.A.Any()
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.A.Any());matches any document where A has 1 or more items.
This is translated to the following MongoDB query:
{ A : { $ne : null, $not : { $size : 0 } } }Any With Predicate
This method is used to test entries in an array. It will generate an $elemMatch condition.
var query =
from c in collection.AsQueryable<C>()
where c.A.Any(a => a.B == 1)
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.A.Any(a => a.B == 1));This is translated to the following MongoDB query:
{ A : { $elemMatch : { B : 1 } } }NOTE
This will only function when the elements of the enumerable are serialized as a document. There is a server bug preventing this from working against primitives.
Boolean constant
This form is mostly for completeness. You will probably use it rarely. It allows a boolean constant to be used to either match or not match the document.
var query =
from c in collection.AsQueryable<C>()
where true
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => true);This is translated to the following MongoDB query:
{ _id : { $exists : true } }Which matches all documents since the _id is a mandatory field.
Boolean field or property
A boolean field or property of the document doesn’t have to be compared to true, it can just be mentioned in the where clause and there is an implied comparison to true.
var query =
from c in collection.AsQueryable<C>()
where c.B
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.B);This is translated to the following MongoDB query:
{ B : true }Contains (Enumerable method)
There are 2 uses for this method depending on context.
To test whether an array (or array-like) field or property contains a particular value:
var query =
from c in collection.AsQueryable<C>()
where c.A.Contains(123)
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.A.Contains(123));This is translated to the following MongoDB query:
{ A : 123 }This translation relies on the way array fields are treated by the MongoDB query language.
To test whether a field or property is contained in an array (or array-like) field.
var local = new [] { 1, 2, 3 }; var query =
from c in collection.AsQueryable<C>()
where local.Contains(c.A)
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => local.Contains(c.A));This is translated to the following MongoDB query:
{ A : { $in : [1, 2, 3] } }
Contains (string method)
This method is used to test whether a string field or property of the document contains a particular substring.
var query =
from c in collection.AsQueryable<C>()
where c.S.Contains("abc")
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.S.Contains("abc"));This is translated to the following MongoDB query (using regular expressions):
{ S : /abc/ }ContainsAll (LINQ to MongoDB extension method)
This method is used to test whether an array (or array-like) field or property contains all of the provided values.
var query =
from c in collection.AsQueryable<C>()
where c.A.ContainsAll(new[] { 1, 2, 3 })
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.A.ContainsAll(new[] { 1, 2, 3 }));This is translated to the following MongoDB query:
{ A : { $all : [1, 2, 3] } }ContainsAny (LINQ to MongoDB extension method)
This method is used to test whether an array (or array-like) field or property contains any of the provided values.
var query =
from c in collection.AsQueryable<C>()
where c.A.ContainsAny(new[] { 1, 2, 3 })
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.A.ContainsAny(new[] { 1, 2, 3 }));This is translated to the following MongoDB query:
{ A : { $in : [1, 2, 3] } }Count method (array length)
This method is used to test whether an enumerable field or property has a certain count of items.
var query =
from c in collection.AsQueryable<C>()
where c.L.Count() == 3
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.L.Count() == 3);This is translated to the following MongoDB query:
{ L : { $size: 3 } }Count property (array length)
This property is used to test whether a list (or list-like) field or property has a certain count of items.
var query =
from c in collection.AsQueryable<C>()
where c.L.Count == 3
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.L.Count == 3);This is translated to the following MongoDB query:
{ L : { $size: 3 } }EndsWith (string method)
This method is used to test whether a string field or property of the document ends with a particular substring.
var query =
from c in collection.AsQueryable<C>()
where c.S.EndsWith("abc")
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.S.EndsWith("abc"));This is translated to the following MongoDB query (using regular expressions):
{ S : /abc$/ }enum comparisons (==, !=, <, <=, >, >=)
enum fields or properties can be compared to constants of the same enum type. The relative comparison are based on the value of the underlying integer type.
public enum E { None, A, B }; var query =
from c in collection.AsQueryable<C>()
where c.E == E.A
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.E == E.A);This is translated to the following MongoDB query:
{ E : 1 }The LINQ implementation takes the representation of serialized values into account, so if you have configured your class map to store enums as string values instead of integer values the MongoDB query would instead be:
{ E : "A" }GetType (Type method)
This is exactly like the OfType method. It will generate a discriminator “and”ed with the other predicates.
var query =
from c in collection.AsQueryable<C>()
where c.GetType() == typeof(D)
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.GetType() == typeof(D));This is translated roughly to the following MongoDB query depending on how your discriminators are created.
{ _t : "D" }In (LINQ to MongoDB extension method)
The In method is used to test whether a field or property is equal any of a set of provided values.
var query =
from c in collection.AsQueryable<C>()
where c.X.In(new [] { 1, 2, 3 })
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c.X.In(new [] { 1, 2, 3 }));This is translated to the following MongoDB query:
{ X : { $in : [1, 2, 3] } }Inject
Inject is a pseudo-method that is used to inject a lower level MongoDB query into a LINQ query. The following query looks for X values that are larger than 0 and are 64-bit integers.
var query =
from c in collection.AsQueryable<C>()
where c.X > 0 && Query.Type("X", BsonType.Int64).Inject()
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.X > 0 && Query.Type("X", BsonType.Int64).Inject());This is translated to the following MongoDB query:
{ X : { $gt : 0, $type : 18 } }is C# keyword
This is exactly like the OfType method. It will generate a discriminator “and”ed with the other predicates.
var query =
from c in collection.AsQueryable<C>()
where c is D && ((D)c).B == 1
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c is D && ((D)c).B == 1);This is translated to the something similar to the following, depending on how your discriminators are setup.
{ _t : "D", B : 1 }IsMatch (regular expression method)
This method is used to test whether a string field or property matches a regular expression.
var regex = new Regex("^abc");
var query =
from c in collection.AsQueryable<C>()
where regex.IsMatch(c.S)
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => regex.IsMatch(c.S));This is translated to the following MongoDB query:
{ S : /^abc/ }You can also use the static IsMatch method.
var query =
from c in collection.AsQueryable<C>()
where Regex.IsMatch(c.S, "^abc")
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => Regex.IsMatch(c.S, "^abc"));This is translated to the following MongoDB query:
{ S : /^abc/ }Length (array length)
This method is used to test whether an array (or array-like) field or property has a certain count of items.
var query =
from c in collection.AsQueryable<C>()
where c.A.Length == 3
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.A.Length == 3);This is translated to the following MongoDB query:
{ A : { $size: 3 } }% (Mod operator)
This operator is used to test the result of the mod operator against a field or property of the document. The following query matches all the documents where X is odd.
var query =
from c in collection.AsQueryable<C>()
where c.X % 2 == 1
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.X % 2 == 1);This is translated to the following MongoDB query:
{ X : { $mod : [2, 1] } }! (Not operator)
The ! operator is used to reverse the sense of a test.
var query =
from c in collection.AsQueryable<C>()
where !(c.X > 1)
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => !(c.X > 1));This is translated into the following MongoDB query:
{ X : { $not : { $gt : 1 } } }NOTE
!(c.X > 1) is not equivalent to (c.X <= 1) in cases where c.X is missing or does not have a numeric type.
Numeric comparisons (==, !=, <, <=, >, >=)
Numeric fields or properties can be compared using any of the above operators.
var query =
from c in collection.AsQueryable<C>()
where c.X == 0 && c.Y < 100
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.X == 0 && c.Y < 100);This is translated into the following MongoDB query:
{ X : 0, Y : { $lt : 100 } }|| (Or operator)
Sub-expressions can be combined with the || operator to test whether any of them is true.
var query =
from c in collection.AsQueryable<C>()
where c.X > 0 || c.Y > 0
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.X > 0 || c.Y > 0);This is translated to the following MongoDB query:
{ $or : [{ X : { $gt : 0 } }, { Y : { $gt : 0 } }] }StartsWith (string method)
This method is used to test whether a string field or property of the document starts with a particular substring.
var query =
from c in collection.AsQueryable<C>()
where c.S.StartsWith("abc")
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.S.StartsWith("abc"));This is translated to the following MongoDB query (using regular expressions):
{ S : /^abc/ }ToLower, ToLowerInvariant, ToUpper, ToUpperInvariant (string method)
These methods are used to test whether a string field or property of the document matches a value in a case-insensitive manner.
var query =
from c in collection.AsQueryable<C>()
where c.S.ToLower() == "abc"
select c;
// or
var query =
collection.AsQueryable<C>()
.Where(c => c.S.ToLower() == "abc"); This is translated to the following MongoDB query (using regular
expressions): .. code-block:: javascript { S : /^abc$/i }
C# Driver LINQ Tutorial的更多相关文章
- MongoDB C# / .NET Driver
MongoDB C# Driver是官方提供的.NET C#驱动. Getting Started with the C# Driver C# Driver Tutorial C# Driver LI ...
- MongoDB .Net Driver(C#驱动) - 内嵌数组/嵌入文档的操作(增加、删除、修改、查询(Linq 分页))
目录 一.前言 1. 运行环境 二.前期准备工作 1. 创建 MongoDBContext MongoDb操作上下文类 2.创建测试类 3.创建测试代码 三.内嵌数组增加元素操作 1.Update.S ...
- Linq 动态查询排序
Linq的排序一般是这样写的: query.OrderBy(x => x.Tel).Skip().Take(); 实际使用中排序字段可能是通过字符类型的参数来设置的,于是想这样实现: query ...
- MongoDB驱动之Linq操作
添加下面命名空间到您的程序中: using MongoDB.Driver.Linq; 声明一变量保存对集合的引用 var collection = database.GetCollection< ...
- Getting Started with the C# Driver
1.下载 如果下载的.zip文件,只需要解压即可. 如果安装的.msi文件,它会将C#驱动DLL放在C:\Program Files (x86)\MongoDB\CSharp Driver xxx的位 ...
- C# mongoDB Driver 使用对象方式查询语法大全
#region 查询方法 /// <summary> /// 获取单个对象 /// </summary> /// <typeparam name="T" ...
- c#LINQ表达树
如果你已经用过LINQ, 你应该知道函数方式,以及包含的丰富的类库, 如果你仍不了解, 那根据下面的链接去熟悉一下 the LINQ tutorial, lambda. 表达树提供了丰富的包含参数的 ...
- MongoDB--CSharp Driver Quickstart .
原文链接 http://www.mongodb.org/display/DOCS/CSharp+Driver+Quickstart?showComments=true&showCommentA ...
- MongoDB入门及 c# .netcore客户端MongoDB.Driver使用
MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系 ...
随机推荐
- Java基础——数组Array
一.数组基本概念 数组是相同类型数据的有序集合. 数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成.其中,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问它们. 数组有三 ...
- 网页flv下载探索_1
最近看了一个优酷视频(非优酷网站,最终地址指向优酷),用chrome开发者工具,可找到flv地址如下,简单摘录如下: http://27.221.100.104/657D4D2878C3382C781 ...
- LuaExpat笔记
xml xml是一种格式化数据交换语言, 适用于在网络上不同应用会话. http://www.xml.com/pub/a/98/10/guide0.html exPat 一种C语言实现的 xml 文档 ...
- (转)@SuppressWarnings的使用、作用、用法
在java编译过程中会出现很多警告,有很多是安全的,但是每次编译有很多警告影响我们对error的过滤和修改,我们可以在代码中加上 @SuppressWarnings(“XXXX”) 来解决 例如:@S ...
- [原创]java WEB学习笔记73:Struts2 学习之路-- strut2中防止表单重复提交
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- WOW: 宏
1.常用的宏命令 1.1常用的宏命令 1.释放技能命令 /cast 释放一个或多个技能,可以加入一些条件判断,是最常用的命令 /castsequence 依次释放释放数个技能,同样可以加入一些条件判断 ...
- bzoj1834 [ZJOI2010]network 网络扩容
第一问跑最大流,第二问新建一条边连接0和1,流量为上第一问的答案+k,费用为0,接下来图中每条边拆成两条边,第一条容量为C费用为0,第二条容量无穷费用为W,再跑一遍费用流即可. 代码 #include ...
- CCF真题之窗口
201403-2 问题描述 在某图形操作系统中,有 N 个窗口,每个窗口都是一个两边与坐标轴分别平行的矩形区域.窗口的边界上的点也属于该窗口.窗口之间有层次的区别,在多于一个窗口重叠的区域里,只会显示 ...
- c#读取INI文件类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;na ...
- paper 92:Lena与图像处理
如果你是个数字图像处理技术的研究人员,或这方面的工程师, 想必对这位戴有羽状帽饰的美女不会感觉陌生.我第一次在国际知名的学术性刊物上见到这位美女时,不禁叹为天人,毕竟在这样严肃的学术期刊上,还从未出现 ...