0

I am trying to sort different field. i.e. blogID, date, author I found order by method but dont know how to pass the value in the method... help please

public List<blog> BlogFetch(string key)
{
    List<blog> blogRes = new List<blog>();

    using (var be = new BlogEntities())
    {
        blog res = new blog();

        foreach (var User in be.blogs.OrderBy(<what goes here?>))
        {
            res = User;
            blogRes.Add(res);
        }
    }
    return blogRes;
}

2 Answers 2

3
be.blogs.OrderBy(b => b.NameOfPropertyOnBlogClass)

It's a Lambda expression which is used heavily in LINQ. Here's a good "cheatsheet" on getting started with LINQ syntax.

As an aside if you want to load a List of items when using EF you can just call ToList().

Sign up to request clarification or add additional context in comments.

Comments

2

The OrderBy method takes a function with the following signature:

TResult PropertyRetrieval<TInput, TResult>(TInput obj);

So, if you have a Blog and you want to sort by BlogID, the result will be this (assuming BlogID is an int):

int SortBy(Blog obj)
{
    return obj.BlogId;
}

You can enter this by a Lambda expression:

be.blogs.OrderBy(blog => blog.BlogID); 
be.blogs.OrderBy(blog => blog.Date);
be.blogs.OrderBy(blog => blog.Author);

etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.