如何解决如何使用 mongo 查询语法对 C# 中的字段进行动态排序
我是 MongoDB 的新手。所以我可以用 c# 和 linq 格式进行排序。 但是不能在 mongosyntax 中完成整个想法是我正在编写一个 mongo shell 查询,我也想在我的 c# 代码中使用相同的语法。
这里 方法 1:不起作用
public async List<T> getGridData(Filter filter,sort sort,int page size,int currentPage){
await collection.Find(FilterDefinition<Student>.Empty)
.Skip((currentPage - 1) * pageSize)
.Limit(pageSize)
.Sort("{LastName: 1}") **// here it is static i want to pass dynaimc column name instead of last name**
.ForEachAsync(
student =>
{
Console.WriteLine($"S/N: {count},\t Id: {student.Id},FirstName: {student.FirstName},LastName: {student.LastName},Age: {student.Age}");
count++;
});
}
这里我可以使用 mongo 语法,但列名像 last 一样硬编码
Ascending:
.Sort("{LastName: 1}") I need to change .Sort("{sort.columnName: 1}")
Descending:
.Sort("{LastName: -1}") I need to change .Sort("{sort.columnName: -1}") but it detects as string.
这是当前的列名。 方法 2:// 工作正常 我在 C# 驱动程序 mongo 语法中实现了同样的功能;
await collection.Find(FilterDefinition<Student>.Empty)
.Skip((currentPage - 1) * pageSize)
.Limit(pageSize)
.Sort(Builders<Student>.Sort.Descending(sort.ColumnName))
.ForEachAsync(
student =>
{
Console.WriteLine($"S/N: {count},Age: {student.Age}");
count++;
});
否则我也可以使用相同的语法来实现: 方法 3:// 工作正常
await collection.Find(FilterDefinition<Student>.Empty)
.Skip((currentPage - 1) * pageSize)
.Limit(pageSize)
.Sort(Builders<Student>.SortByDescending(x => nameof(sort.columnName))
.ForEachAsync(
student =>
{
Console.WriteLine($"S/N: {count},Age: {student.Age}");
count++;
});
但是整个想法如何在方法 1 语法中传递动态列,其中 mongo shell 脚本语法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。