如何使用ORDER BY在这个MSDN例子 [英] How to use Order By in this MSDN example

查看:187
本文介绍了如何使用ORDER BY在这个MSDN例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何使用这个orderBy参数。我不知道什么,我猜想传递

<一个href=\"http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application\">http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

 公共虚拟IEnumerable的&LT; TEntity&GT;得到(
        防爆pression&LT;&Func键LT; TEntity,布尔&GT;&GT;过滤器= NULL,
        FUNC&LT;&IQueryable的LT; TEntity&gt;中IOrderedQueryable&LT; TEntity&GT;&GT; ORDERBY = NULL,
        串includeProperties =)
    {
        IQueryable的&LT; TEntity&GT;查询= dbSet;        如果(过滤器!= NULL)
        {
            查询= query.Where(过滤器);
        }        在includeProperties.Split的foreach(VAR includeProperty
            (新的char [] {','},StringSplitOptions.RemoveEmptyEntries))
        {
            查询= query.Include(includeProperty);
        }        如果(排序依据!= NULL)
        {
            返回的OrderBy(查询).ToList();
        }
        其他
        {
            返回query.ToList();
        }
    }


解决方案

如果您从MSDN文章阅读

的code Func键,IOrderedQueryable> ORDERBY也意味着主叫方会提供一个lambda前pression。但在这种情况下,输入到EX pression是TEntity类型一个IQueryable对象。这位前pression将在调用方法返回的IQueryable对象的有序版本。例如,如果存储库实例为学生实体类型,code可能指定q => q.OrderBy(S => s.LastName)为orderBy参数。

它说,当你调用Get你应该提供一个Lambda前pression这将是对的IQueryable提供IOrderedQueryable一个Func键或功能。

所以对于文章中使用的Student对象,你螨使用。

  VAR学生= repository.Get(X =&GT; x.FirstName =鲍勃,Q =&GT; q.OrderBy(S = GT; s.LastName));

I'm trying to figure out how to use this orderBy parameter. I am not sure what I am suppose to pass in.

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

   public virtual IEnumerable<TEntity> Get(
        Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includeProperties = "")
    {
        IQueryable<TEntity> query = dbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            return orderBy(query).ToList();
        }
        else
        {
            return query.ToList();
        }
    }

解决方案

If you read this from the msdn article

"The code Func, IOrderedQueryable> orderBy also means the caller will provide a lambda expression. But in this case, the input to the expression is an IQueryable object for the TEntity type. The expression will return an ordered version of that IQueryable object. For example, if the repository is instantiated for the Student entity type, the code in the calling method might specify q => q.OrderBy(s => s.LastName) for the orderBy parameter."

Its saying when you call the Get you should provide a lambda expression which will be a Func or function on the IQueryable providing an IOrderedQueryable.

So for the Student object used in the article you mite use.

var students = repository.Get(x => x.FirstName = "Bob",q => q.OrderBy(s => s.LastName));

这篇关于如何使用ORDER BY在这个MSDN例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆