Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

c# - How to use an order by parameter in a generic function method call

I am new to using generics. Very powerful stuff but a bit confusing to me and I'm trying to understand how to use the following code:

    public async Task<IEnumerable<TEntity>> Find(Expression<Func<TEntity, bool>> predicate, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderExpression = null)
    {
        try
        {
            return await this.GetQuery(predicate, orderExpression);
        }
        catch (Exception e)
        {
            return null;
        }
    }

    public async Task<IQueryable<TEntity>> GetQuery(Expression<Func<TEntity, bool>> predicate = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderExpression = null)
    {
        IQueryable<TEntity> qry = this.DBSet;

        if (predicate != null)
            qry = qry.Where(predicate);

        if (orderExpression != null)
            return orderExpression(qry);


        return qry;
    }

I am able to call the find method with a predicate like so:

@code {
    [Parameter]
    public string CurrentID { get; set; }
    Repository2<User> repository2 = new Repository2<User>();
    IEnumerable<User> users;
    User user = new User();

    protected override async Task OnInitializedAsync()
    {
        users = await Task.Run(() => repository2.Find(u => u.ID == Convert.ToInt32(CurrentID)));
        user = users.SingleOrDefault();
    }

    protected void UpdateUser()
    {
        repository2.Update<User>(user);
        repository2.SaveChanges();
        navidationMananger.NavigateTo("Users");
    }

    void Cancel()
    {
        navidationMananger.NavigateTo("Users");
    }

}
   

However I am unable to figure out how use the OrderBy functionality of the above Find method.

How would I call this method passing an order by as the second parameter? If you could explain it a bit so I understand why it works that would be most appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Its LINQ syntax, so you can call OrderBy on the output of the Find since it returns either an IEnumerable or IQueryable. Below is one example. Or you could return it as an object, then call OrderBy or OrderByDescending later on. Its usually best to do them together though, because of Lazy Loading, which defers execution on those operations until it actually needs to be manifested, either looped through or returned as a concrete type, like a List, as opposed to an IEnumerable. Make sense?

@code {
    [Parameter]
    public string CurrentID { get; set; }
    Repository2<User> repository2 = new Repository2<User>();
    IEnumerable<User> users;
    User user = new User();

    protected override async Task OnInitializedAsync()
    {
        users = await Task.Run(() => repository2
            .FindAll(u => u.ID == Convert.ToInt32(CurrentID)).OrderBy(v => v.ID));
        user = users.SingleOrDefault();
    }

    protected void UpdateUser()
    {
        repository2.Update<User>(user);
        repository2.SaveChanges();
        navidationMananger.NavigateTo("Users");
    }

    void Cancel()
    {
        navidationMananger.NavigateTo("Users");
    }

}

Or you could simplify the call to this:

        user = await Task.Run(() => repository2
            .FindAll(u => u.ID == Convert.ToInt32(CurrentID))
            .OrderBy(v => v.ID).FirstOrDefault());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...