/// <summary>
 /// Retrieves collection of data objects.
 /// </summary>
 /// <returns>
 /// Collection of generic data objects. Empty collection is returned
 /// if no data objects found.
 /// </returns>
 protected IEnumerable <T> FindAllObjects <T, TEntity>()
     where T : DataObject
     where TEntity : EntityObject
 {
     return(FindObjects <T, TEntity>(
                SqlFormatHelper.FormatObjName(_entitySet)));
 }
        protected IEnumerable <T> FindObjects <T, TEntity>(Expression <Func <TEntity, bool> > whereClause)
            where T : DataObject
            where TEntity : EntityObject
        {
            Debug.Assert(whereClause != null);

            // get objects query
            ObjectQuery <TEntity> query = _context.CreateQuery <TEntity>(SqlFormatHelper.FormatObjName(_entitySet));

            // find objects
            IQueryable <TEntity> resultQuery = query.Where(whereClause);

            List <T> list = new List <T>(_FindObjects <T, TEntity>(resultQuery));

            return(list.AsReadOnly());
        }
        /// <summary>
        /// Removes all data objects from object context.
        /// </summary>
        protected void RemoveAllObjects <TEntity>()
            where TEntity : EntityObject
        {
            // remove data object from cache
            //_context.Cache.Clear();

            // remove entity objects from context
            // EF 1.0 does not support RemoveAll functionality
            // TODO: use entity SQL
            ObjectQuery <TEntity> query = _context.CreateQuery <TEntity>(
                SqlFormatHelper.FormatObjName(_entitySet));

            foreach (TEntity entity in query)
            {
                _context.DeleteObject(entity);
            }
        }
        /// <summary>
        /// Returns orders planned on a date from the range, which string properties contain
        /// the keyword.
        /// </summary>
        /// <param name="startDate">Start date used to query orders.</param>
        /// <param name="endDate">Finish date used to query orders.</param>
        /// <param name="keyword">The keyword to find.</param>
        /// <param name="asSynchronized">Indicates whether collection remains syncronized
        /// when orders are added or deleted to the project database.</param>
        /// <returns>Syncronized collection of found unassigned orders for the schedule.</returns>
        /// <remarks>
        /// <para>Method searches in the following order properties: <c>Name</c>, <c>Address</c>,
        /// <c>CustomProperties</c></para>
        /// <para>Collection isn't automatically updated when some of the found orders
        /// properties changes.</para>
        /// </remarks>
        public IDataObjectCollection <Order> SearchByKeyword(DateTime startDate,
                                                             DateTime endDate,
                                                             string keyword,
                                                             bool asSynchronized)
        {
            Debug.Assert(keyword != null);

            Expression <Func <DataModel.Orders, bool> > filter =
                ((DataModel.Orders order) =>
                 ((order.PlannedDate >= startDate && order.PlannedDate < endDate) &&
                  ((order.Name != null && order.Name.Contains(keyword)) ||
                   (order.FullAddress != null && order.FullAddress.Contains(keyword)) ||
                   (order.Unit != null && order.Unit.Contains(keyword)) ||
                   (order.AddressLine != null && order.AddressLine.Contains(keyword)) ||
                   (order.Locality1 != null && order.Locality1.Contains(keyword)) ||
                   (order.Locality2 != null && order.Locality2.Contains(keyword)) ||
                   (order.Locality3 != null && order.Locality3.Contains(keyword)) ||
                   (order.CountyPrefecture != null && order.CountyPrefecture.Contains(keyword)) ||
                   (order.PostalCode1 != null && order.PostalCode1.Contains(keyword)) ||
                   (order.PostalCode2 != null && order.PostalCode2.Contains(keyword)) ||
                   (order.StateProvince != null && order.StateProvince.Contains(keyword)) ||
                   (order.Country != null && order.Country.Contains(keyword)) ||
                   (order.CustomProperties != null && order.CustomProperties.Contains(keyword)))));

            var qp = new ObjectParameter[]
            {
                new ObjectParameter("start_date", startDate),
                new ObjectParameter("end_date", endDate),
                new ObjectParameter("keyword",
                                    "%" + SqlFormatHelper.EscapeLikeString(keyword, '~') + "%")
            };

            Expression <Func <DataModel.Orders, bool> > expression = asSynchronized ? filter : null;

            return(_SearchOrders(ORDERS_BY_KEYWORD, qp, expression));
        }
        ///////////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////

        public ObjectQuery <TEntity> CreateDefaultQuery <TEntity>()
        {
            return(_context.CreateQuery <TEntity>(
                       SqlFormatHelper.FormatObjName(_entitySet)));
        }