Sort data using EF DbSet

Is there a way to pass an order by clause to a DbSet class in EF?

I am using c #

+5
source share
5 answers

I'm not sure how to do this with DbSet, but you can do it from DBContext by gaining access to IQueryable

private readonly DbContext context;
...
context.Set<T>().OrderBy(item => item.Property)
+12
source

Here db.Employees is a DBSet. Is this what you are trying to do?

using System.Linq;

namespace MyNamespace
{
    public class MyClass
    {
        public DBContext db;
        public void MyMethod
        {
            foreach (var emp in db
                .Employees
                .Where(e => e.IsActive) // (or whatever)
                .OrderBy(e => e.LastName))
            {
                DoSomething(emp);
            }
        }
    }
}
+3
source

, :

var emps = from e in _db.Employees
            orderby e.FirstName
            select e;

_db.Employees - DbSet.

+2

.OrderBy Entity?

db.Employees.OrderBy(p => p.Id);
+2

,

QueryView Edmx.

QueryView // .

: DefiningQuery QueryView

+1
source

All Articles