Joh*_*han 657 c# linq linq-to-objects
我在动态LINQ 的VS2008示例中找到了一个示例,它允许您使用类似sql的字符串(例如,OrderBy("Name, Age DESC"))用于排序.不幸的是,包含的方法仅适用于IQueryable<T>;.有没有办法获得此功能IEnumerable<T>?
Mar*_*ell 890
只是偶然发现这个老人......
要在没有动态LINQ库的情况下执行此操作,您只需要以下代码即可.这涵盖了大多数常见方案,包括嵌套属性.
为了让它与IEnumerable<T>你一起工作,可以添加一些经过的包装器方法AsQueryable- 但下面的代码是Expression所需的核心逻辑.
public static IOrderedQueryable<T> OrderBy<T>(
this IQueryable<T> source,
string property)
{
return ApplyOrder<T>(source, property, "OrderBy");
}
public static IOrderedQueryable<T> OrderByDescending<T>(
this IQueryable<T> source,
string property)
{
return ApplyOrder<T>(source, property, "OrderByDescending");
}
public static IOrderedQueryable<T> ThenBy<T>(
this IOrderedQueryable<T> source,
string property)
{
return ApplyOrder<T>(source, property, "ThenBy");
}
public static IOrderedQueryable<T> ThenByDescending<T>(
this IOrderedQueryable<T> source,
string property)
{
return ApplyOrder<T>(source, property, "ThenByDescending");
}
static IOrderedQueryable<T> ApplyOrder<T>(
IQueryable<T> source,
string property,
string methodName)
{
string[] props = property.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach(string prop in props) {
// use reflection (not ComponentModel) to mirror LINQ
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
object result = typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] {source, lambda});
return (IOrderedQueryable<T>)result;
}
Run Code Online (Sandbox Code Playgroud)
编辑:如果你想混合使用它会变得更有趣dynamic- 虽然注意dynamic只适用于LINQ-to-Objects(ORMs的表达式树等不能真正代表dynamic查询 - MemberExpression不支持它).但是这里有一种方法可以使用LINQ-to-Objects.请注意,选择Hashtable是由于有利的锁定语义:
using Microsoft.CSharp.RuntimeBinder;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Runtime.CompilerServices;
static class Program
{
private static class AccessorCache
{
private static readonly Hashtable accessors = new Hashtable();
private static readonly Hashtable callSites = new Hashtable();
private static CallSite<Func<CallSite, object, object>> GetCallSiteLocked(
string name)
{
var callSite = (CallSite<Func<CallSite, object, object>>)callSites[name];
if(callSite == null)
{
callSites[name] = callSite = CallSite<Func<CallSite, object, object>>
.Create(Binder.GetMember(
CSharpBinderFlags.None,
name,
typeof(AccessorCache),
new CSharpArgumentInfo[] {
CSharpArgumentInfo.Create(
CSharpArgumentInfoFlags.None,
null)
}));
}
return callSite;
}
internal static Func<dynamic,object> GetAccessor(string name)
{
Func<dynamic, object> accessor = (Func<dynamic, object>)accessors[name];
if (accessor == null)
{
lock (accessors )
{
accessor = (Func<dynamic, object>)accessors[name];
if (accessor == null)
{
if(name.IndexOf('.') >= 0) {
string[] props = name.Split('.');
CallSite<Func<CallSite, object, object>>[] arr
= Array.ConvertAll(props, GetCallSiteLocked);
accessor = target =>
{
object val = (object)target;
for (int i = 0; i < arr.Length; i++)
{
var cs = arr[i];
val = cs.Target(cs, val);
}
return val;
};
} else {
var callSite = GetCallSiteLocked(name);
accessor = target =>
{
return callSite.Target(callSite, (object)target);
};
}
accessors[name] = accessor;
}
}
}
return accessor;
}
}
public static IOrderedEnumerable<dynamic> OrderBy(
this IEnumerable<dynamic> source,
string property)
{
return Enumerable.OrderBy<dynamic, object>(
source,
AccessorCache.GetAccessor(property),
Comparer<object>.Default);
}
public static IOrderedEnumerable<dynamic> OrderByDescending(
this IEnumerable<dynamic> source,
string property)
{
return Enumerable.OrderByDescending<dynamic, object>(
source,
AccessorCache.GetAccessor(property),
Comparer<object>.Default);
}
public static IOrderedEnumerable<dynamic> ThenBy(
this IOrderedEnumerable<dynamic> source,
string property)
{
return Enumerable.ThenBy<dynamic, object>(
source,
AccessorCache.GetAccessor(property),
Comparer<object>.Default);
}
public static IOrderedEnumerable<dynamic> ThenByDescending(
this IOrderedEnumerable<dynamic> source,
string property)
{
return Enumerable.ThenByDescending<dynamic, object>(
source,
AccessorCache.GetAccessor(property),
Comparer<object>.Default);
}
static void Main()
{
dynamic a = new ExpandoObject(),
b = new ExpandoObject(),
c = new ExpandoObject();
a.X = "abc";
b.X = "ghi";
c.X = "def";
dynamic[] data = new[] {
new { Y = a },
new { Y = b },
new { Y = c }
};
var ordered = data.OrderByDescending("Y.X").ToArray();
foreach (var obj in ordered)
{
Console.WriteLine(obj.Y.X);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Ala*_*sta 224
没有任何复杂情况太容易了:
using System.Linq.Dynamic;在顶部添加.vehicles = vehicles.AsQueryable().OrderBy("Make ASC, Year DESC").ToList();Ada*_*son 52
偶然发现了这个问题.
使用上面的Marc的ApplyOrder实现,我把一个处理类似SQL的字符串的Extension方法拼凑在一起,如:
list.OrderBy("MyProperty DESC, MyOtherProperty ASC");
Run Code Online (Sandbox Code Playgroud)
详情请见:http://aonnull.blogspot.com/2010/08/dynamic-sql-like-linq-orderby-extension.html
Kje*_*dal 41
我想使用反射来获得你想要排序的任何属性是有用的:
IEnumerable<T> myEnumerables
var query=from enumerable in myenumerables
where some criteria
orderby GetPropertyValue(enumerable,"SomeProperty")
select enumerable
private static object GetPropertyValue(object obj, string property)
{
System.Reflection.PropertyInfo propertyInfo=obj.GetType().GetProperty(property);
return propertyInfo.GetValue(obj, null);
}
Run Code Online (Sandbox Code Playgroud)
请注意,使用反射比直接访问属性要慢得多,因此必须调查性能.
vdh*_*ant 18
只是建立在别人所说的基础上.我发现以下效果很好.
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;
}
Run Code Online (Sandbox Code Playgroud)
Inf*_*tus 11
我绊倒了这个问题,寻找Linq多个orderby子句,也许这就是作者所寻求的
以下是如何做到这一点:
var query = pets.OrderBy(pet => pet.Name).ThenByDescending(pet => pet.Age);
Run Code Online (Sandbox Code Playgroud)
Jam*_*ack 11
我试图这样做,但遇到了Kjetil Watnedal解决方案的问题,因为我不使用内联linq语法 - 我更喜欢方法式语法.我的具体问题是尝试使用自定义进行动态排序IComparer.
我的解决方案最终如下:
给出一个像这样的IQueryable查询:
List<DATA__Security__Team> teams = TeamManager.GetTeams();
var query = teams.Where(team => team.ID < 10).AsQueryable();
Run Code Online (Sandbox Code Playgroud)
并给出了运行时排序字段参数:
string SortField; // Set at run-time to "Name"
Run Code Online (Sandbox Code Playgroud)
动态OrderBy看起来像这样:
query = query.OrderBy(item => item.GetReflectedPropertyValue(SortField));
Run Code Online (Sandbox Code Playgroud)
这是使用一个名为GetReflectedPropertyValue()的辅助方法:
public static string GetReflectedPropertyValue(this object subject, string field)
{
object reflectedValue = subject.GetType().GetProperty(field).GetValue(subject, null);
return reflectedValue != null ? reflectedValue.ToString() : "";
}
Run Code Online (Sandbox Code Playgroud)
最后一件事 - 我提到我想要OrderBy使用自定义IComparer- 因为我想做自然排序.
要做到这一点,我只需OrderBy改为:
query = query.OrderBy(item => item.GetReflectedPropertyValue(SortField), new NaturalSortComparer<string>());
Run Code Online (Sandbox Code Playgroud)
请参阅此帖子以获取代码NaturalSortComparer().
使用动态 linq
只需添加 using System.Linq.Dynamic;
并像这样使用它来订购所有列:
string sortTypeStr = "ASC"; // or DESC
string SortColumnName = "Age"; // Your column name
query = query.OrderBy($"{SortColumnName} {sortTypeStr}");
Run Code Online (Sandbox Code Playgroud)
经过大量搜索,这对我有用:
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);
}
Run Code Online (Sandbox Code Playgroud)
小智 6
首先安装动态 工具 --> NuGet 包管理器 --> 包管理器控制台
install-package System.Linq.Dynamic
Run Code Online (Sandbox Code Playgroud)
添加命名空间 using System.Linq.Dynamic;
现在你可以使用OrderBy("Name, Age DESC")
你可以添加它:
public static IEnumerable<T> OrderBy( this IEnumerable<T> input, string queryString) {
//parse the string into property names
//Use reflection to get and sort by properties
//something like
foreach( string propname in queryString.Split(','))
input.OrderBy( x => GetPropertyValue( x, propname ) );
// I used Kjetil Watnedal's reflection example
}
Run Code Online (Sandbox Code Playgroud)
该GetPropertyValue功能来自Kjetil Watnedal 的回答
问题是为什么?任何此类排序都会在运行时抛出异常,而不是编译时(如 D2VIANT 的答案)。
如果您正在处理 Linq to Sql 并且 orderby 是一个表达式树,那么无论如何它都会被转换为 SQL 来执行。