我在VS2008动态LINQ示例中找到了一个示例,允许您使用类似sql的字符串(例如OrderBy(“Name, Age DESC”))进行排序。不幸的是,所包含的方法只适用于IQueryable<T>。有什么办法得到这个功能IEnumerable<T>?
当前回答
经过大量的搜索,这对我来说很有效:
public static IEnumerable<TEntity> OrderBy<TEntity>(this IEnumerable<TEntity> source,
string orderByProperty, bool desc)
{
string command = desc ? "OrderByDescending" : "OrderBy";
var type = typeof(TEntity);
var property = type.GetProperty(orderByProperty);
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExpression = Expression.Lambda(propertyAccess, parameter);
var resultExpression = Expression.Call(typeof(Queryable), command,
new[] { type, property.PropertyType },
source.AsQueryable().Expression,
Expression.Quote(orderByExpression));
return source.AsQueryable().Provider.CreateQuery<TEntity>(resultExpression);
}
其他回答
我在寻找Linq多序子句时遇到了这个问题 也许这就是作者想要的
下面是如何做到这一点:
var query = pets.OrderBy(pet => pet.Name).ThenByDescending(pet => pet.Age);
只是在别人说的基础上。我发现下面的方法效果很好。
public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> input, string queryString)
{
if (string.IsNullOrEmpty(queryString))
return input;
int i = 0;
foreach (string propname in queryString.Split(','))
{
var subContent = propname.Split('|');
if (Convert.ToInt32(subContent[1].Trim()) == 0)
{
if (i == 0)
input = input.OrderBy(x => GetPropertyValue(x, subContent[0].Trim()));
else
input = ((IOrderedEnumerable<T>)input).ThenBy(x => GetPropertyValue(x, subContent[0].Trim()));
}
else
{
if (i == 0)
input = input.OrderByDescending(x => GetPropertyValue(x, subContent[0].Trim()));
else
input = ((IOrderedEnumerable<T>)input).ThenByDescending(x => GetPropertyValue(x, subContent[0].Trim()));
}
i++;
}
return input;
}
首次安装动态 工具——> NuGet包管理器——>包管理器控制台
install-package System.Linq.Dynamic
使用System.Linq.Dynamic添加Namespace;
现在您可以使用OrderBy(“名称,年龄DESC”)
你可以这样做,对多个顺序
IOrderedEnumerable<JToken> sort;
if (query.OrderBys[0].IsDESC)
{
sort = jarry.OrderByDescending(r => (string)r[query.OrderBys[0].Key]);
}
else
{
sort = jarry.OrderBy(r =>
(string) r[query.OrderBys[0].Key]);
}
foreach (var item in query.OrderBys.Skip(1))
{
if (item.IsDESC)
{
sort = sort.ThenByDescending(r => (string)r[item.Key]);
}
else
{
sort = sort.ThenBy(r => (string)r[item.Key]);
}
}
如果您正在使用规范(例如Ardalis规范)
using Microsoft.EntityFrameworkCore;
namespace TestExtensions;
public static class IQueryableExtensions
{
public static void ApplyOrder<T>(ISpecificationBuilder<T> query, string propertyName, bool ascendingOrder)
{
if (ascendingOrder)
query.OrderBy(T => EF.Property<object>(T!, propertyName));
else
query.OrderByDescending(T => EF.Property<object>(T!, propertyName));
}
}
推荐文章
- 流。Seek(0, SeekOrigin.Begin)或Position = 0
- 如果查询为空,则最大返回值
- "输出类型为类库的项目不能直接启动"
- 传递一个实例化的系统。类型作为泛型类的类型参数
- 从SqlCommand对象获取生成的SQL语句?
- 把内容放在HttpResponseMessage对象?
- 在c#中转换字符串为类型
- 如何比较单元测试中的列表
- 替换c#字符串中的多个字符
- 将XML字符串转换为对象
- ToList()——它是否创建一个新列表?
- 连接网络共享时,如何提供用户名和密码
- ProcessStartInfo挂在“WaitForExit”?为什么?
- 窗口vs页面vs用户控件的WPF导航?
- Task和Task的区别是什么?启动/等待和异步/等待?