views:

33

answers:

1

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

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.

Craig Stuntz
Does this mean I can't compile ObjectQuery queries? Or is there a way? I wanted to use both dynamic sorting and paging without using Expression Trees and precompiling them. Can't I do both?
Armagan
Sure you can compile `ObjectQuery`. You just can't compile an expression using Query Builder methods. You also can't compile a "dynamic" query. Think about it; it's a contradiction in terms! Compiled queries can vary only by parameters, not by syntax.
Craig Stuntz