将 IOrderedQueryable 添加到 IQueryable
我有一种正在尝试制作的通用方法,为了实现这一点,我想将iorderedqueryable作为参数。
public async Task<IEnumerable<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> filter, IOrderedQueryable orderBy){
IQueryable<TEntity> query = dbSet;
query = query.Where(filter);
query = query.OrderBy(orderBy); //This is what I want to do
return await query.ToListAsync();
}
我想从外部传递一个iorderedqueryable,以便能够根据字符串制作功能,并能够查看我的对象的属性(没有反射),对于我的每个对象。
public static Func<IQueryable<MyObject>, string, IOrderedQueryable<Entity>> OrderBy = (query, orderBy) =>
{
return orderBy switch
{
"Id" => query.OrderBy(x => x.Id),
"Prop1" => query.OrderBy(x => x.Prop1),
"Prop2" => query.OrderBy(x => Prop2),
_ => query.OrderBy(x => x.Id)
};
};
我不知道是否会更改订单是否会产生任何影响。
query = orderBy;
query = query.Where(filter);
或者我不知道我是否可以做concat,还是会让我失去我的条款
query = query.Concat(orderBy);
I have this generic method that I'm trying to make, and in order to achieve that I want to pass an IOrderedQueryable as a parameter.
public async Task<IEnumerable<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> filter, IOrderedQueryable orderBy){
IQueryable<TEntity> query = dbSet;
query = query.Where(filter);
query = query.OrderBy(orderBy); //This is what I want to do
return await query.ToListAsync();
}
I want to pass an IOrderedQueryable from outside in order to be able to make the function depending on a string and be able to look at the properties of my object (without reflection), for one and each of my objects.
public static Func<IQueryable<MyObject>, string, IOrderedQueryable<Entity>> OrderBy = (query, orderBy) =>
{
return orderBy switch
{
"Id" => query.OrderBy(x => x.Id),
"Prop1" => query.OrderBy(x => x.Prop1),
"Prop2" => query.OrderBy(x => Prop2),
_ => query.OrderBy(x => x.Id)
};
};
I don't know if I change the order if it would have any impact.
query = orderBy;
query = query.Where(filter);
Or I don't know if I can do a Concat, or if that would make me lose my where clause
query = query.Concat(orderBy);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论