我有一个集合,其中的标题可以是多种语言
示例:如果我的平台语言是西班牙语,所有标题都将是西班牙语。那么如何使用Title
排序呢?public class Collection
{
public string Id { get; set; }
public string Title { get; set; }
}
有两种排序方式,
- Mongo Linq
- 流畅聚合语法
对于这两种情况,您必须使用AggregateOptions和Set Collation。本地到你想要的本地,你也可以切换其他标志来获得适当的案例排序。
collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
For MongoDb Linq
var option = new AggregateOptions
{
Collation = new Collation(searchParam.Local, false)
};
var list = collection.AsQueryable(option).OrderBy(x=>x.Title).
ToListAsync(ct);
For Fluent Aggregate Syntax
var option = new AggregateOptions
{
Collation = new Collation(searchParam.Local, false)
};
var result collection.Aggregate(option).Match(searchFilter).
Sort(Builders<Collection>.Sort.Ascending(x=>x.Title).
ToListAsync(ct);