Sort data using entity model

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;
}

Answers


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().


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.


Need Your Help

C returning array in function

c arrays pointers

Im relatively knew to C, i am used to program in Java so i find C a little bit difficult in what concerns arrays. I still cofuse myself with this cases:

How to return an superclass object in a subclass's instance method?

java oop

The following is a toy problem of my original problem. Bird class is an interface. Cardinal is the subclass of the Point class and it implements Bird interface. The Aviary class carries out