本文介绍了EF OrderBy dinamically的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过EF使用orderby,通过字符串传递参数:

I am trying to use a orderby via EF, passing the parameter via string:




public static List< Book> ListBy(字符串字段)

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;列表与LT;图书> books = context.Books.OrderByDescending(m => m.GetType()。GetProperty(field).GetValue(m,null)。ToList();

       返还书籍;

    }

public static List<Book> ListBy(string field)
        {
            List<Book> books = context.Books.OrderByDescending(m => m.GetType().GetProperty(field).GetValue(m, null).ToList();
            return books;
        }

然而,它不起作用。如果它只是一个字段,我会执行:



However, it's not working. If it was just a field, I would do:

       List< Book> books = context.Books .OrderByDescending(m => m.BookId)。ToList();

            List<Book> books = context.Books.OrderByDescending(m => m.BookId).ToList();

我该如何改编呢?

Thx

How can I adapt that ?
Thx

推荐答案

> ;>我如何适应?

因为数据库不支持c#方法,你可以将记录加载到内存中并使用c#方法。像这样:

Because database does not support c# method, which you could load the records to memory and use c# method. like this:

public static List<Book> ListBy(string field)
         {
             List<Book> books = context.Books.ToList().OrderByDescending(m => m.GetType().GetProperty(field).GetValue(m, null).ToList();
             return books;
         }

或者您也可以使用以下扩展来实现它。

Or you could also use the following extension to achieve it.

public static partial class QueryableExtensions
{
    public static IOrderedQueryable<T> OrderByMember<T>(this IQueryable<T> source, string memberPath)
    {
        return source.OrderByMemberUsing(memberPath, "OrderBy");
    }
    public static IOrderedQueryable<T> OrderByMemberDescending<T>(this IQueryable<T> source, string memberPath)
    {
        return source.OrderByMemberUsing(memberPath, "OrderByDescending");
    }
    public static IOrderedQueryable<T> ThenByMember<T>(this IOrderedQueryable<T> source, string memberPath)
    {
        return source.OrderByMemberUsing(memberPath, "ThenBy");
    }
    public static IOrderedQueryable<T> ThenByMemberDescending<T>(this IOrderedQueryable<T> source, string memberPath)
    {
        return source.OrderByMemberUsing(memberPath, "ThenByDescending");
    }
    private static IOrderedQueryable<T> OrderByMemberUsing<T>(this IQueryable<T> source, string memberPath, string method)
    {
        var parameter = Expression.Parameter(typeof(T), "item");
        var member = memberPath.Split('.')
            .Aggregate((Expression)parameter, Expression.PropertyOrField);
        var keySelector = Expression.Lambda(member, parameter);
        var methodCall = Expression.Call(
            typeof(Queryable), method, new[] { parameter.Type, member.Type },
            source.Expression, Expression.Quote(keySelector));
        return (IOrderedQueryable<T>)source.Provider.CreateQuery(methodCall);
    }
}

有关详细信息,请参阅:

For more information, please refer to:

https://stackoverflow.com/questions/39908403 /怎么用-a-string-to-create-a-ef-order-by-expression / 39916384#39916384

祝你好运,

Cole Wu


这篇关于EF OrderBy dinamically的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 01:35