views:

24

answers:

2

I've written compiled queries for my ObjectContext which I use as select methods in a ObjectDataSource. The proble is that I now want to implement sorting and paging and I'd like to it in data layer which means I need to add .OrderBy and .Take/.Skip to my compiled queries. Does this make sense? I mean won't the compiled query recompile every time any parameter changes? Should go back to regular Linq to Entities queries? It should be something like this:

namespace BLL
{
    [DataObject(true)]
    public class CompanyManager
    {
        private static readonly Func<DBContext, IEnumerable<CompanyInfo>> companyList;

        static CompanyManager()
        {
            if (companyList== null)
            {
                companyList= CompiledQuery.Compile<DBContext, IEnumerable<CompanyInfo>>(
                        (DBContext vt) =>
                            from row in vt.Companies
                            select new CompanyInfo()
                            {
                                ...
                            }
                    );
            }
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static List<CompanyInfo> CompanyList()
        {
            List<CompanyInfo> list= new List<CompanyInfo>();
            using (var vt = DBContext.Instance())
            {
                //ORDERBY AND THE REST WILL BE ADDED
                list.AddRange(companyList(vt)).OrderBy(row => row.CompanyName).Skip(20).Take(10);
            }
            return list;
        }
    }
}

ads

A: 

The Compile method has other overloads. Why not do

companyListOrdered = CompiledQuery.Compile<DBContext, int, int, IEnumerable<CompanyInfo>>( 
        (DBContext vt, int start, int length ) => 
            from row in vt.Companies.OrderBy(row => row.CompanyName).Skip(start).Take(length)
            select new CompanyInfo() 
            { 
                ... 
            } 
    ); 

Then something like

 list.AddRange(companyListOrdered(vt, 20, 10));

Of course you'd need a different compiled query for every OrderBy clause, but I don't see any way around this.

luksan