.netentity-frameworkentity-sql

Dynamically sorting and filtering projected Entity SQL results


I have the following query which I want to sort or filter using projected class names:

List<CompanyInfo> list = new List<CompanyInfo>();

using (var db = new DbContext())
{
    list.AddRange(
                  db.Companies.Include("Projects")
                  .Select(row => new CompanyInfo()
                  {
                      ProjectCount = (from s in row.Projects
                                      where s.Company.fId == row.fId
                                      select s.pId).Count(),
                      Id = satir.fId,
                  })
                 //.OrderBy("ProjectCount") //<== what I want to do
            );
}

I want to dynamically order this query using ProjectCount or Id columns same as ESQL, like .OrderBy("ProjectCount"). Since the query result is IQueryable instead of ObjectContext it doesn't work. Is there a way to do this?


Solution

  • Thanks for the replies, I used helper extension methods:

    Sorting in IQueryable using string as column name