Search code examples
.netentity-frameworkentity-sql

Compiled Entity SQL query and OrderBy


I'm trying to come up with an compiled query using Entity SQL and I'm getting this error on ToList() line:

LINQ to Entities does not recognize the method 'System.Data.Objects.ObjectQuery`1[BLL.Company] OrderBy(System.String, System.Data.Objects.ObjectParameter[])' method, and this method cannot be translated into a store expression.

Here's the code I'm trying:

class param
{
    public string where, orderby;
    public int skip, take;

}

..

public class BLL.CompanyManager
{
    private static readonly Func<DbContext, param, IQueryable<CompanyInfo>> companyList;

    static MyClass()
    {
        companyList = CompiledQuery.Compile<DbContext, param, IQueryable<CompanyInfo>>(
                        (DbContext db, param p) =>
                            db.Companies.Include("Projects")
                            //.Where(p.where)
                            .OrderBy(p.orderby)
                            .Skip(p.skip)
                            .Take(p.take)
                            .Select(row => new CompanyInfo()
                            {
                ...
                            })
                    );

        public static List<CompanyInfo> CompanyList(..)
        {
            ...
            List<CompanyInfo> list = new List<CompanyInfo>();
            using (var db = (DbContext.Instance())
            {
                list.AddRange(companyList(db, params).ToList<CompanyInfo>()); // <== ERROR
            }
            return list;
        }
    }
}

Solution

  • Like it says, you can't use a query builder method in an L2E query. The distinction is subtle, because they look identical, but query builder and L2E aren't the same thing.