C# 如何将Linq投影与IMongoQueryable结合使用<;张力>;在C中#
我在MongoDB.Driver v2.9.2中使用C#8、.net标准2.0。我正在将一个项目从RavenDB迁移到MongoDB 我需要在GetAllAsync方法中使用IMongoQueryable的include,但我弄糊涂了。这里是方法C# 如何将Linq投影与IMongoQueryable结合使用<;张力>;在C中#,c#,mongodb,mongodb-query,C#,Mongodb,Mongodb Query,我在MongoDB.Driver v2.9.2中使用C#8、.net标准2.0。我正在将一个项目从RavenDB迁移到MongoDB 我需要在GetAllAsync方法中使用IMongoQueryable的include,但我弄糊涂了。这里是方法 public async Task<ICollection<TEntity>> GetAllAsync<TOrderBy>(Expression<Func<TEntity, TOrderBy>>
public async Task<ICollection<TEntity>> GetAllAsync<TOrderBy>(Expression<Func<TEntity, TOrderBy>> orderBy,
bool isDescendingOrder = false,
Expression<Func<TEntity, object>> projection = null,
CancellationToken cancellationToken = default)
{
IMongoQueryable<TEntity> mongoQueryable = Collection.AsQueryable();
if (isDescendingOrder)
{
return await mongoQueryable.OrderByDescending(orderBy)
.ToListAsync(cancellationToken: cancellationToken);
}
return await mongoQueryable.OrderBy(orderBy)
.ToListAsync(cancellationToken: cancellationToken);
}
公共异步任务GetAllAsync(表达式orderBy,
bool isDescendingOrder=false,
表达式投影=null,
CancellationToken CancellationToken=默认值)
{
IMongoQueryable mongoQueryable=Collection.AsQueryable();
如果(isDescendingOrder)
{
return wait mongoQueryable.OrderByDescending(orderBy)
.ToListSync(cancellationToken:cancellationToken);
}
return wait mongoQueryable.OrderBy(OrderBy)
.ToListSync(cancellationToken:cancellationToken);
}
到目前为止我所尝试的;我可以使用“Find()”方法生成相同的查询,但是
SortByDescending方法需要“Expression orderBy
”-无法像我提供的上部代码块那样使用Expression orderBy
Builders<TEntity>.Sort.Ascending(orderBy).
Func<TEntity, TOrderBy> compile = orderBy.Compile();
Collection.Find(new BsonDocument()).Project(projection).SortByDescending(compile).ToListAsync();
Builders.Sort.升序(orderBy)。
Func compile=orderBy.compile();
Collection.Find(new-BsonDocument()).Project(projection).SortByDescending(compile).toListSync();
最后一件事,我是如何在RavenDB解决这个问题的:
public async Task<ICollection<TEntity>> GetAllAsync<TOrderBy>(Expression<Func<TEntity, TOrderBy>> orderBy,
bool isDescendingOrder = false,
Expression<Func<TEntity, object>> projection = null,
CancellationToken cancellationToken = default)
{
ICollection<TEntity> entities;
IRavenQueryable<TEntity> query = AsyncDocumentSession.Query<TEntity>();
if (projection != null)
{
query = query.Include(projection);
}
if (isDescendingOrder)
{
entities = await query.OrderByDescending(orderBy)
.ToListAsync(token: cancellationToken);
}
else
{
entities = await query.OrderBy(orderBy)
.ToListAsync(token: cancellationToken);
}
return entities;
}
公共异步任务GetAllAsync(表达式orderBy,
bool isDescendingOrder=false,
表达式投影=null,
CancellationToken CancellationToken=默认值)
{
i收集实体;
IRavenQueryable query=AsyncDocumentSession.query();
if(投影!=null)
{
query=query.Include(投影);
}
如果(isDescendingOrder)
{
实体=等待查询。OrderByDescending(orderBy)
.ToListSync(令牌:取消令牌);
}
其他的
{
实体=等待查询.OrderBy(OrderBy)
.ToListSync(令牌:取消令牌);
}
返回实体;
}
尝试以下方法:
公共静态异步任务GetAllAsync
(
表达式orderBy,
bool isDescendingOrder=false,
表达式投影=null,
CancellationToken CancellationToken=默认值
)
{
var query=_collection.AsQueryable();
var sortedQuery=isDescendingOrder?
query.OrderByDescending(orderBy):
query.OrderBy(OrderBy);
查询=投影!=空?
sortedQuery.选择(投影):
选择(x=>x);
返回wait query.toListSync(cancellationToken)作为ICollection;
}
测试程序:
使用系统;
使用System.Collections.Generic;
使用System.Linq.Expressions;
使用系统线程;
使用System.Threading.Tasks;
使用MongoDB.Entities;
使用MongoDB.Entities.Core;
使用MongoDB.Driver.Linq;
使用MongoDB.Driver;
命名空间堆栈溢出
{
公共类人员:实体
{
公共字符串名称{get;set;}
公共整数{get;set;}
}
公共静态类程序
{
公共异步静态任务Main()
{
新DB(“测试”);
wait(newperson{Name=“Person”,Age=22});
var结果=
等待GetAllAsync(
p=>p.年龄,
是的,
p=>新人{Name=p.Name,Age=p.Age});
}
公共静态异步任务GetAllAsync
(
表达式orderBy,
bool isDescendingOrder=false,
表达式投影=null,
CancellationToken CancellationToken=默认值
)
{
var query=DB.Queryable();
var sortedQuery=isDescendingOrder?
query.OrderByDescending(orderBy):
query.OrderBy(OrderBy);
查询=投影!=空?
sortedQuery.选择(投影):
选择(x=>x);
返回wait query.toListSync(cancellationToken)作为ICollection;
}
}
}
您能展示一下您的方法的示例用法吗?projection
应该表示像x=>x.MyField
这样将作为输出返回的单个字段,还是您想指定更多字段?谢谢您的帮助,我对您的答案投了赞成票,但接口方法对projection有Expression projection=null
,所以它不是表达式投影=null
。你有任何理由使用“tenty”而不是“object”吗?应该是坦蒂吗?对不起,我错了。它也在使用Expression projection=null
,不需要tenty,tenty。但是应该是什么呢?我是说,最好的做法是什么?使用“object”或使用“TEntity”作为第二种类型的表达式函数?@Lost_在_库中使用TEntity
如果希望投影的输出类型与输入类型相同。或者使用object
作为返回类型,以便能够投影到任何类型。例如x=>new{Prop=x.Prop}
或x=>newsometype{Prop=x.Prop}
@Lost_In_Library我之所以将其设置为TEntity
是因为函数的返回类型是ICollection
,这意味着您将不会投影到