Mic*_*lle 3 c# linq sorting entity-framework
我正在尝试首先过滤一组产品,然后订购它们
(repository.GetAllProducts()
.Where(x=>x.Naam.Contains("filter")) as ObjectQuery<Product>)
.FunkyOrder<Product>(sortfield,sortdirection)
Run Code Online (Sandbox Code Playgroud)
使用Extension方法:
public static IEnumerable<T> FunkyOrder<T>(this ObjectQuery<T> input, string fieldname = "Id", string sortdirection = "asc")
{
switch (sortdirection)
{
case "dsc":
case "desc":
return input.OrderBy("it." + fieldname + " DESC");
default:
return input.OrderBy("it." + fieldname);
}
}
Run Code Online (Sandbox Code Playgroud)
(我的Repository的GetAllProducts()返回一个IEnumerable
这个FunkyOrder方法应该可以工作,但是 - 作为ObjectQuery - cast返回NULL
我只能使用带有ObjectQuery的FunkyOrder,因为这似乎是唯一支持那些"it.FieldName"查询的东西.
如果我将 - as - notation更改为常规演员,我得到:
Unable to cast object of type 'WhereEnumerableIterator`1[MySecretNameSpace.Product]' to type 'System.Data.Objects.ObjectQuery`1[MySecretNameSpace.Product]'.
Run Code Online (Sandbox Code Playgroud)
有没有办法让这项工作或者我会被迫在过滤之前使用排序,或者为我的排序编写一个带有lambda表达式的巨型开关?
这实际上是一个试图解决的巨大头痛; 但最终,它实际上很容易通过一些hackishness和反思来完成:
public class Program
{
public class SomeClass
{
public int Id { get; set; }
public string Name { get; set; }
}
static void Main(string[] args)
{
List<SomeClass> sample = new List<SomeClass>
{
new SomeClass { Id = 4, Name = "ABC" },
new SomeClass { Id = 1, Name = "XYZ" },
new SomeClass { Id = 2, Name = "JKL" }
};
var result = sample.OrderByDynamic("Name", OrderDirection.Ascending).ToList();
result.ForEach(x => Console.WriteLine("Id: " + x.Id + " | Name: " + x.Name));
Console.ReadKey();
}
}
public enum OrderDirection
{
Ascending,
Descending
}
public static class LinqExtensions
{
public static IEnumerable<T> OrderByDynamic<T>(this IEnumerable<T> source, string propertyName, OrderDirection direction = OrderDirection.Ascending)
{
if(direction == OrderDirection.Ascending)
return source.OrderBy(x => x.GetType().GetProperty(propertyName).GetValue(x, null));
else
return source.OrderByDescending(x => x.GetType().GetProperty(propertyName).GetValue(x, null));
}
}
Run Code Online (Sandbox Code Playgroud)
这个例子在测试情况下对我有用; 我可以提供属性名称,并指定排序方向,它工作得很好.只需这样打电话:
yourEnumerable.OrderByDynamic("YourPropertyName");
yourEnumerable.OrderByDynamic("YourPropertyName", OrderDirection.Descending);
Run Code Online (Sandbox Code Playgroud)
请享用!