Click here to Skip to main content
15,892,674 members
Articles / Desktop Programming / WPF

Building WPF Applications with Self-Tracking Entity Generator and Visual Studio 2012 - Project Setup

Rate me:
Please Sign up or sign in to vote.
5.00/5 (14 votes)
17 Mar 2013CPOL8 min read 69.1K   3.5K   44  
This article describes the project setup of building a WPF sample application with Self-Tracking Entity Generator and Visual Studio 2012.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Composition;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using SchoolSample.Common;
using SchoolSample.EntityModel;
using SchoolSample.EntityModel.Resource;

namespace SchoolSample.ViewModel
{
    [Export(ViewModelTypes.CoursePageFilterViewModel, typeof (ViewModelBase))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class CoursePageFilterViewModel : ViewModelBase
    {
        #region "Private Data Members"

        private NotificationMessageAction<ClientFilter<Course>> _notification;
        private int _currentMaxSortOrder;

        #endregion "Private Data Members"

        #region "Constructor"

        public CoursePageFilterViewModel()
        {
            // register for CourseFilterMessage
            AppMessages.CourseFilterMessage.Register(this, OnCourseFilterMessage);

            // set enum type
            ListSortDirectionCollection = new EnumCollection<ListSortDirection>(true,
                                                                                SchoolModelResource.ResourceManager);
            JointConditionCollection = new EnumCollection<JointConditionEnum>(false, SchoolModelResource.ResourceManager);

            CompareOperator = new List<string> {">=", ">", "=", "<", "<="};

            ResetAll();
        }

        #endregion "Constructor"

        #region "Public Properties"

        public EnumCollection<ListSortDirection> ListSortDirectionCollection { get; private set; }
        public EnumCollection<JointConditionEnum> JointConditionCollection { get; private set; }
        public List<string> CompareOperator { get; private set; }

        public string CourseName
        {
            get { return _courseName; }
            set
            {
                if (!ReferenceEquals(_courseName, value))
                {
                    _courseName = value;
                    RaisePropertyChanged("CourseName");
                }
            }
        }

        private string _courseName;

        public int CourseNameSortDirectionIndex
        {
            get { return _courseNameSortDirectionIndex; }
            set
            {
                if (_courseNameSortDirectionIndex != value)
                {
                    _courseNameSortDirectionIndex = value;
                    RaisePropertyChanged("CourseNameSortDirectionIndex");
                }
            }
        }

        private int _courseNameSortDirectionIndex;

        public int? CourseNameSortOrder
        {
            get { return _courseNameSortOrder; }
            set
            {
                if (!ReferenceEquals(_courseNameSortOrder, value))
                {
                    _courseNameSortOrder = value;
                    RaisePropertyChanged("CourseNameSortOrder");
                }
            }
        }

        private int? _courseNameSortOrder;

        public string InstructorName
        {
            get { return _instructorName; }
            set
            {
                if (!ReferenceEquals(_instructorName, value))
                {
                    _instructorName = value;
                    RaisePropertyChanged("InstructorName");
                }
            }
        }

        private string _instructorName;

        public int InstructorNameSortDirectionIndex
        {
            get { return _instructorNameSortDirectionIndex; }
            set
            {
                if (_instructorNameSortDirectionIndex != value)
                {
                    _instructorNameSortDirectionIndex = value;
                    RaisePropertyChanged("InstructorNameSortDirectionIndex");
                }
            }
        }

        private int _instructorNameSortDirectionIndex;

        public int? InstructorNameSortOrder
        {
            get { return _instructorNameSortOrder; }
            set
            {
                if (!ReferenceEquals(_instructorNameSortOrder, value))
                {
                    _instructorNameSortOrder = value;
                    RaisePropertyChanged("InstructorNameSortOrder");
                }
            }
        }

        private int? _instructorNameSortOrder;

        public DateTime? CourseStartDate
        {
            get { return _courseStartDate; }
            set
            {
                if (!ReferenceEquals(_courseStartDate, value))
                {
                    _courseStartDate = value;
                    RaisePropertyChanged("CourseStartDate");
                }
            }
        }

        private DateTime? _courseStartDate;

        public int CourseStartDateCompareOperatorIndex
        {
            get { return _courseStartDateCompareOperatorIndex; }
            set
            {
                if (_courseStartDateCompareOperatorIndex != value)
                {
                    _courseStartDateCompareOperatorIndex = value;
                    RaisePropertyChanged("CourseStartDateCompareOperatorIndex");
                }
            }
        }

        private int _courseStartDateCompareOperatorIndex;

        public int CourseStartDateSortDirectionIndex
        {
            get { return _courseStartDateSortDirectionIndex; }
            set
            {
                if (_courseStartDateSortDirectionIndex != value)
                {
                    _courseStartDateSortDirectionIndex = value;
                    RaisePropertyChanged("CourseStartDateSortDirectionIndex");
                }
            }
        }

        private int _courseStartDateSortDirectionIndex;

        public int? CourseStartDateSortOrder
        {
            get { return _courseStartDateSortOrder; }
            set
            {
                if (!ReferenceEquals(_courseStartDateSortOrder, value))
                {
                    _courseStartDateSortOrder = value;
                    RaisePropertyChanged("CourseStartDateSortOrder");
                }
            }
        }

        private int? _courseStartDateSortOrder;

        public DateTime? CourseEndDate
        {
            get { return _courseEndDate; }
            set
            {
                if (!ReferenceEquals(_courseEndDate, value))
                {
                    _courseEndDate = value;
                    RaisePropertyChanged("CourseEndDate");
                }
            }
        }

        private DateTime? _courseEndDate;

        public int CourseEndDateCompareOperatorIndex
        {
            get { return _courseEndDateCompareOperatorIndex; }
            set
            {
                if (_courseEndDateCompareOperatorIndex != value)
                {
                    _courseEndDateCompareOperatorIndex = value;
                    RaisePropertyChanged("CourseEndDateCompareOperatorIndex");
                }
            }
        }

        private int _courseEndDateCompareOperatorIndex;

        public int CourseEndDateSortDirectionIndex
        {
            get { return _courseEndDateSortDirectionIndex; }
            set
            {
                if (_courseEndDateSortDirectionIndex != value)
                {
                    _courseEndDateSortDirectionIndex = value;
                    RaisePropertyChanged("CourseEndDateSortDirectionIndex");
                }
            }
        }

        private int _courseEndDateSortDirectionIndex;

        public int? CourseEndDateSortOrder
        {
            get { return _courseEndDateSortOrder; }
            set
            {
                if (!ReferenceEquals(_courseEndDateSortOrder, value))
                {
                    _courseEndDateSortOrder = value;
                    RaisePropertyChanged("CourseEndDateSortOrder");
                }
            }
        }

        private int? _courseEndDateSortOrder;

        public int? ClassSize
        {
            get { return _classSize; }
            set
            {
                if (!ReferenceEquals(_classSize, value))
                {
                    _classSize = value;
                    RaisePropertyChanged("ClassSize");
                }
            }
        }

        private int? _classSize;

        public int ClassSizeCompareOperatorIndex
        {
            get { return _classSizeCompareOperatorIndex; }
            set
            {
                if (_classSizeCompareOperatorIndex != value)
                {
                    _classSizeCompareOperatorIndex = value;
                    RaisePropertyChanged("ClassSizeCompareOperatorIndex");
                }
            }
        }

        private int _classSizeCompareOperatorIndex;

        public int ClassSizeSortDirectionIndex
        {
            get { return _classSizeSortDirectionIndex; }
            set
            {
                if (_classSizeSortDirectionIndex != value)
                {
                    _classSizeSortDirectionIndex = value;
                    RaisePropertyChanged("ClassSizeSortDirectionIndex");
                }
            }
        }

        private int _classSizeSortDirectionIndex;

        public int? ClassSizeSortOrder
        {
            get { return _classSizeSortOrder; }
            set
            {
                if (!ReferenceEquals(_classSizeSortOrder, value))
                {
                    _classSizeSortOrder = value;
                    RaisePropertyChanged("ClassSizeSortOrder");
                }
            }
        }

        private int? _classSizeSortOrder;

        public int JointCondition1Index
        {
            get { return _jointCondition1Index; }
            set
            {
                if (_jointCondition1Index != value)
                {
                    _jointCondition1Index = value;
                    RaisePropertyChanged("JointCondition1Index");
                }
            }
        }

        private int _jointCondition1Index;

        public int JointCondition2Index
        {
            get { return _jointCondition2Index; }
            set
            {
                if (_jointCondition2Index != value)
                {
                    _jointCondition2Index = value;
                    RaisePropertyChanged("JointCondition2Index");
                }
            }
        }

        private int _jointCondition2Index;

        public int JointCondition3Index
        {
            get { return _jointCondition3Index; }
            set
            {
                if (_jointCondition3Index != value)
                {
                    _jointCondition3Index = value;
                    RaisePropertyChanged("JointCondition3Index");
                }
            }
        }

        private int _jointCondition3Index;

        public int JointCondition4Index
        {
            get { return _jointCondition4Index; }
            set
            {
                if (_jointCondition4Index != value)
                {
                    _jointCondition4Index = value;
                    RaisePropertyChanged("JointCondition4Index");
                }
            }
        }

        private int _jointCondition4Index;

        public bool? DialogResult
        {
            get { return _dialogResult; }
            set
            {
                if (!ReferenceEquals(_dialogResult, value))
                {
                    _dialogResult = value;
                    RaisePropertyChanged("DialogResult");
                }
            }
        }

        private bool? _dialogResult;

        #endregion "Public Properties"

        #region "Public Commands"

        /// <summary>
        /// Command for the OK button
        /// </summary>
        public RelayCommand OkCommand
        {
            get
            {
                if (_okCommand == null)
                {
                    _okCommand = new RelayCommand(
                        OnOKCommand);
                }
                return _okCommand;
            }
        }

        private RelayCommand _okCommand;

        private void OnOKCommand()
        {
            if (_notification != null)
            {
                var clientFilter = ParseClientFilter();

                _notification.Execute(clientFilter);
                DialogResult = true;
            }
        }

        /// <summary>
        /// Command for the Cancel button
        /// </summary>
        public RelayCommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new RelayCommand(
                        OnCancelCommand);
                }
                return _cancelCommand;
            }
        }

        private RelayCommand _cancelCommand;

        private void OnCancelCommand()
        {
            DialogResult = false;
        }

        /// <summary>
        /// Command for the Save as Default button
        /// </summary>
        public RelayCommand SaveDefaultCommand
        {
            get
            {
                if (_saveDefaultCommand == null)
                {
                    _saveDefaultCommand = new RelayCommand(
                        OnSaveDefaultCommand);
                }
                return _saveDefaultCommand;
            }
        }

        private RelayCommand _saveDefaultCommand;

        private void OnSaveDefaultCommand()
        {
            var clientFilter = ParseClientFilter();
            AppMessages.CourseDefaultFilterMessage.Send(clientFilter);
            DialogResult = true;
        }

        /// <summary>
        /// Command for the Reset button
        /// </summary>
        public RelayCommand ResetCommand
        {
            get
            {
                if (_resetCommand == null)
                {
                    _resetCommand = new RelayCommand(
                        OnResetCommand);
                }
                return _resetCommand;
            }
        }

        private RelayCommand _resetCommand;

        private void OnResetCommand()
        {
            ResetAll();
        }

        /// <summary>
        /// Command for SelectionChanged event of CourseName SortDirection
        /// </summary>
        public RelayCommand CourseNameSortDirectionSelectionChangedCommand
        {
            get
            {
                if (_courseNameSortDirectionSelectionChangedCommand == null)
                {
                    _courseNameSortDirectionSelectionChangedCommand = new RelayCommand(
                        OnCourseNameSortDirectionSelectionChangedCommand);
                }
                return _courseNameSortDirectionSelectionChangedCommand;
            }
        }

        private RelayCommand _courseNameSortDirectionSelectionChangedCommand;

        private void OnCourseNameSortDirectionSelectionChangedCommand()
        {
            if (CourseNameSortDirectionIndex == 0)
            {
                if (CourseNameSortOrder != null)
                {
                    var currentInt = CourseNameSortOrder;
                    CourseNameSortOrder = null;
                    _currentMaxSortOrder = _currentMaxSortOrder - 1;

                    if (InstructorNameSortOrder != null && InstructorNameSortOrder > currentInt)
                    {
                        InstructorNameSortOrder = InstructorNameSortOrder - 1;
                    }
                    if (CourseStartDateSortOrder != null && CourseStartDateSortOrder > currentInt)
                    {
                        CourseStartDateSortOrder = CourseStartDateSortOrder - 1;
                    }
                    if (CourseEndDateSortOrder != null && CourseEndDateSortOrder > currentInt)
                    {
                        CourseEndDateSortOrder = CourseEndDateSortOrder - 1;
                    }
                    if (ClassSizeSortOrder != null && ClassSizeSortOrder > currentInt)
                    {
                        ClassSizeSortOrder = ClassSizeSortOrder - 1;
                    }
                }
            }
            else
            {
                if (CourseNameSortOrder == null)
                {
                    _currentMaxSortOrder++;
                    CourseNameSortOrder = _currentMaxSortOrder;
                }
            }
        }

        /// <summary>
        /// Command for SelectionChanged event of InstructorName SortDirection
        /// </summary>
        public RelayCommand InstructorNameSortDirectionSelectionChangedCommand
        {
            get
            {
                if (_instructorNameSortDirectionSelectionChangedCommand == null)
                {
                    _instructorNameSortDirectionSelectionChangedCommand = new RelayCommand(
                        OnInstructorNameSortDirectionSelectionChangedCommand);
                }
                return _instructorNameSortDirectionSelectionChangedCommand;
            }
        }

        private RelayCommand _instructorNameSortDirectionSelectionChangedCommand;

        private void OnInstructorNameSortDirectionSelectionChangedCommand()
        {
            if (InstructorNameSortDirectionIndex == 0)
            {
                if (InstructorNameSortOrder != null)
                {
                    var currentInt = InstructorNameSortOrder;
                    InstructorNameSortOrder = null;
                    _currentMaxSortOrder = _currentMaxSortOrder - 1;

                    if (CourseNameSortOrder != null && CourseNameSortOrder > currentInt)
                    {
                        CourseNameSortOrder = CourseNameSortOrder - 1;
                    }
                    if (CourseStartDateSortOrder != null && CourseStartDateSortOrder > currentInt)
                    {
                        CourseStartDateSortOrder = CourseStartDateSortOrder - 1;
                    }
                    if (CourseEndDateSortOrder != null && CourseEndDateSortOrder > currentInt)
                    {
                        CourseEndDateSortOrder = CourseEndDateSortOrder - 1;
                    }
                    if (ClassSizeSortOrder != null && ClassSizeSortOrder > currentInt)
                    {
                        ClassSizeSortOrder = ClassSizeSortOrder - 1;
                    }
                }
            }
            else
            {
                if (InstructorNameSortOrder == null)
                {
                    _currentMaxSortOrder++;
                    InstructorNameSortOrder = _currentMaxSortOrder;
                }
            }
        }

        /// <summary>
        /// Command for SelectionChanged event of CourseStartDate SortDirection
        /// </summary>
        public RelayCommand CourseStartDateSortDirectionSelectionChangedCommand
        {
            get
            {
                if (_courseStartDateSortDirectionSelectionChangedCommand == null)
                {
                    _courseStartDateSortDirectionSelectionChangedCommand = new RelayCommand(
                        OnCourseStartDateSortDirectionSelectionChangedCommand);
                }
                return _courseStartDateSortDirectionSelectionChangedCommand;
            }
        }

        private RelayCommand _courseStartDateSortDirectionSelectionChangedCommand;

        private void OnCourseStartDateSortDirectionSelectionChangedCommand()
        {
            if (CourseStartDateSortDirectionIndex == 0)
            {
                if (CourseStartDateSortOrder != null)
                {
                    var currentInt = CourseStartDateSortOrder;
                    CourseStartDateSortOrder = null;
                    _currentMaxSortOrder = _currentMaxSortOrder - 1;

                    if (CourseNameSortOrder != null && CourseNameSortOrder > currentInt)
                    {
                        CourseNameSortOrder = CourseNameSortOrder - 1;
                    }
                    if (InstructorNameSortOrder != null && InstructorNameSortOrder > currentInt)
                    {
                        InstructorNameSortOrder = InstructorNameSortOrder - 1;
                    }
                    if (CourseEndDateSortOrder != null && CourseEndDateSortOrder > currentInt)
                    {
                        CourseEndDateSortOrder = CourseEndDateSortOrder - 1;
                    }
                    if (ClassSizeSortOrder != null && ClassSizeSortOrder > currentInt)
                    {
                        ClassSizeSortOrder = ClassSizeSortOrder - 1;
                    }
                }
            }
            else
            {
                if (CourseStartDateSortOrder == null)
                {
                    _currentMaxSortOrder++;
                    CourseStartDateSortOrder = _currentMaxSortOrder;
                }
            }
        }

        /// <summary>
        /// Command for SelectionChanged event of CourseEndDate SortDirection
        /// </summary>
        public RelayCommand CourseEndDateSortDirectionSelectionChangedCommand
        {
            get
            {
                if (_courseEndDateSortDirectionSelectionChangedCommand == null)
                {
                    _courseEndDateSortDirectionSelectionChangedCommand = new RelayCommand(
                        OnCourseEndDateSortDirectionSelectionChangedCommand);
                }
                return _courseEndDateSortDirectionSelectionChangedCommand;
            }
        }

        private RelayCommand _courseEndDateSortDirectionSelectionChangedCommand;

        private void OnCourseEndDateSortDirectionSelectionChangedCommand()
        {
            if (CourseEndDateSortDirectionIndex == 0)
            {
                if (CourseEndDateSortOrder != null)
                {
                    var currentInt = CourseEndDateSortOrder;
                    CourseEndDateSortOrder = null;
                    _currentMaxSortOrder = _currentMaxSortOrder - 1;

                    if (CourseNameSortOrder != null && CourseNameSortOrder > currentInt)
                    {
                        CourseNameSortOrder = CourseNameSortOrder - 1;
                    }
                    if (InstructorNameSortOrder != null && InstructorNameSortOrder > currentInt)
                    {
                        InstructorNameSortOrder = InstructorNameSortOrder - 1;
                    }
                    if (CourseStartDateSortOrder != null && CourseStartDateSortOrder > currentInt)
                    {
                        CourseStartDateSortOrder = CourseStartDateSortOrder - 1;
                    }
                    if (ClassSizeSortOrder != null && ClassSizeSortOrder > currentInt)
                    {
                        ClassSizeSortOrder = ClassSizeSortOrder - 1;
                    }
                }
            }
            else
            {
                if (CourseEndDateSortOrder == null)
                {
                    _currentMaxSortOrder++;
                    CourseEndDateSortOrder = _currentMaxSortOrder;
                }
            }
        }

        /// <summary>
        /// Command for SelectionChanged event of ClassSize SortDirection
        /// </summary>
        public RelayCommand ClassSizeSortDirectionSelectionChangedCommand
        {
            get
            {
                if (_classSizeSortDirectionSelectionChangedCommand == null)
                {
                    _classSizeSortDirectionSelectionChangedCommand = new RelayCommand(
                        OnClassSizeSortDirectionSelectionChangedCommand);
                }
                return _classSizeSortDirectionSelectionChangedCommand;
            }
        }

        private RelayCommand _classSizeSortDirectionSelectionChangedCommand;

        private void OnClassSizeSortDirectionSelectionChangedCommand()
        {
            if (ClassSizeSortDirectionIndex == 0)
            {
                if (ClassSizeSortOrder != null)
                {
                    var currentInt = ClassSizeSortOrder;
                    ClassSizeSortOrder = null;
                    _currentMaxSortOrder = _currentMaxSortOrder - 1;

                    if (CourseNameSortOrder != null && CourseNameSortOrder > currentInt)
                    {
                        CourseNameSortOrder = CourseNameSortOrder - 1;
                    }
                    if (InstructorNameSortOrder != null && InstructorNameSortOrder > currentInt)
                    {
                        InstructorNameSortOrder = InstructorNameSortOrder - 1;
                    }
                    if (CourseStartDateSortOrder != null && CourseStartDateSortOrder > currentInt)
                    {
                        CourseStartDateSortOrder = CourseStartDateSortOrder - 1;
                    }
                    if (CourseEndDateSortOrder != null && CourseEndDateSortOrder > currentInt)
                    {
                        CourseEndDateSortOrder = CourseEndDateSortOrder - 1;
                    }
                }
            }
            else
            {
                if (ClassSizeSortOrder == null)
                {
                    _currentMaxSortOrder++;
                    ClassSizeSortOrder = _currentMaxSortOrder;
                }
            }
        }

        #endregion "Public Commands"

        #region "Private Methods"

        /// <summary>
        /// Message handler for CourseFilterMessage
        /// </summary>
        /// <param name="notification"></param>
        private void OnCourseFilterMessage(NotificationMessageAction<ClientFilter<Course>> notification)
        {
            _notification = notification;
        }

        /// <summary>
        /// Reset all to initial settings
        /// </summary>
        private void ResetAll()
        {
            CourseName = string.Empty;
            InstructorName = string.Empty;
            CourseStartDate = null;
            CourseEndDate = null;
            ClassSize = null;

            CourseNameSortOrder = null;
            InstructorNameSortOrder = null;
            CourseStartDateSortOrder = null;
            CourseEndDateSortOrder = null;
            ClassSizeSortOrder = null;

            CourseNameSortDirectionIndex = 0;
            InstructorNameSortDirectionIndex = 0;
            CourseStartDateCompareOperatorIndex = 0;
            CourseEndDateCompareOperatorIndex = 0;
            ClassSizeCompareOperatorIndex = 0;

            JointCondition1Index = 0;
            JointCondition2Index = 0;
            JointCondition3Index = 0;
            JointCondition4Index = 0;

            CourseStartDateSortDirectionIndex = 0;
            CourseEndDateSortDirectionIndex = 0;
            ClassSizeSortDirectionIndex = 0;

            _currentMaxSortOrder = 0;
        }

        /// <summary>
        /// Parse and create ClientFilter object
        /// </summary>
        /// <returns></returns>
        private ClientFilter<Course> ParseClientFilter()
        {
            var clientFilter = new ClientFilter<Course>();
            // dynamically build WhereExpression
            // filter by Course Name
            if (!string.IsNullOrEmpty(CourseName))
            {
                clientFilter = clientFilter.And(n => n.Title.Contains(CourseName));
            }

            // filter by Instructor Name
            var clientFilter1 = new ClientFilter<Course>();
            if (!string.IsNullOrEmpty(InstructorName))
            {
                clientFilter1 = clientFilter1.And(n => n.Instructor.Name.Contains(InstructorName));
            }
            clientFilter = JointCondition1Index == 0
                               ? clientFilter.And(clientFilter1.WhereExpression) // Joint Condition : And
                               : clientFilter.Or(clientFilter1.WhereExpression); // Joint Condition : Or

            // filter by Course Start Date
            clientFilter1 = new ClientFilter<Course>();
            if (CourseStartDate != null)
            {
                switch (CourseStartDateCompareOperatorIndex)
                {
                    case 0: // >=
                        clientFilter1 = clientFilter1.And(
                            n => n.StartDate >=
                                 ((DateTime) CourseStartDate).Date);
                        break;
                    case 1: // >
                        clientFilter1 = clientFilter1.And(
                            n => n.StartDate >=
                                 ((DateTime) CourseStartDate).Date.AddDays(1));
                        break;
                    case 2: // =
                        clientFilter1 = clientFilter1.And(
                            n => n.StartDate >=
                                 ((DateTime) CourseStartDate).Date &&
                                 n.StartDate < ((DateTime) CourseStartDate).Date.AddDays(1));
                        break;
                    case 3: // <
                        clientFilter1 = clientFilter1.And(
                            n => n.StartDate <
                                 ((DateTime) CourseStartDate).Date);
                        break;
                    case 4: // <=
                        clientFilter1 = clientFilter1.And(
                            n => n.StartDate <
                                 ((DateTime) CourseStartDate).Date.AddDays(1));
                        break;
                }
            }
            clientFilter = JointCondition2Index == 0
                               ? clientFilter.And(clientFilter1.WhereExpression) // Joint Condition : And
                               : clientFilter.Or(clientFilter1.WhereExpression); // Joint Condition : Or

            // filter by Course End Date
            clientFilter1 = new ClientFilter<Course>();
            if (CourseEndDate != null)
            {
                switch (CourseEndDateCompareOperatorIndex)
                {
                    case 0: // >=
                        clientFilter1 = clientFilter1.And(
                            n => n.EndDate >=
                                 ((DateTime) CourseEndDate).Date);
                        break;
                    case 1: // >
                        clientFilter1 = clientFilter1.And(
                            n => n.EndDate >=
                                 ((DateTime) CourseEndDate).Date.AddDays(1));
                        break;
                    case 2: // =
                        clientFilter1 = clientFilter1.And(
                            n => n.EndDate >=
                                 ((DateTime) CourseEndDate).Date &&
                                 n.EndDate < ((DateTime) CourseEndDate).Date.AddDays(1));
                        break;
                    case 3: // <
                        clientFilter1 = clientFilter1.And(
                            n => n.EndDate <
                                 ((DateTime) CourseEndDate).Date);
                        break;
                    case 4: // <=
                        clientFilter1 = clientFilter1.And(
                            n => n.EndDate <
                                 ((DateTime) CourseEndDate).Date.AddDays(1));
                        break;
                }
            }
            clientFilter = JointCondition3Index == 0
                               ? clientFilter.And(clientFilter1.WhereExpression) // Joint Condition : And
                               : clientFilter.Or(clientFilter1.WhereExpression); // Joint Condition : Or

            // filter by ClassSize
            clientFilter1 = new ClientFilter<Course>();
            if (ClassSize != null)
            {
                switch (ClassSizeCompareOperatorIndex)
                {
                    case 0: // >=
                        clientFilter1 = clientFilter1.And(
                            n => n.ClassSize >= ClassSize);
                        break;
                    case 1: // >
                        clientFilter1 = clientFilter1.And(
                            n => n.ClassSize > ClassSize);
                        break;
                    case 2: // =
                        clientFilter1 = clientFilter1.And(
                            n => n.ClassSize == ClassSize);
                        break;
                    case 3: // <
                        clientFilter1 = clientFilter1.And(
                            n => n.ClassSize < ClassSize);
                        break;
                    case 4: // <=
                        clientFilter1 = clientFilter1.And(
                            n => n.ClassSize <= ClassSize);
                        break;
                }
            }
            clientFilter = JointCondition4Index == 0
                               ? clientFilter.And(clientFilter1.WhereExpression) // Joint Condition : And
                               : clientFilter.Or(clientFilter1.WhereExpression); // Joint Condition : Or

            // dynamically build SortExpressions
            if (CourseNameSortOrder != null && CourseNameSortOrder == 1)
            {
                clientFilter = CourseNameSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.Title) // Ascending
                                   : clientFilter.OrderByDescending(n => n.Title); // Descending
            }
            else if (InstructorNameSortOrder != null && InstructorNameSortOrder == 1)
            {
                clientFilter = InstructorNameSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.Instructor.Name) // Ascending
                                   : clientFilter.OrderByDescending(n => n.Instructor.Name); // Descending
            }
            else if (CourseStartDateSortOrder != null && CourseStartDateSortOrder == 1)
            {
                clientFilter = CourseStartDateSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.StartDate) // Ascending
                                   : clientFilter.OrderByDescending(n => n.StartDate); // Descending
            }
            else if (CourseEndDateSortOrder != null && CourseEndDateSortOrder == 1)
            {
                clientFilter = CourseEndDateSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.EndDate) // Ascending
                                   : clientFilter.OrderByDescending(n => n.EndDate); // Descending
            }
            else if (ClassSizeSortOrder != null && ClassSizeSortOrder == 1)
            {
                clientFilter = ClassSizeSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.ClassSize) // Ascending
                                   : clientFilter.OrderByDescending(n => n.ClassSize); // Descending
            }

            if (CourseNameSortOrder != null && CourseNameSortOrder == 2)
            {
                clientFilter = CourseNameSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.Title) // Ascending
                                   : clientFilter.OrderByDescending(n => n.Title); // Descending
            }
            else if (InstructorNameSortOrder != null && InstructorNameSortOrder == 2)
            {
                clientFilter = InstructorNameSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.Instructor.Name) // Ascending
                                   : clientFilter.OrderByDescending(n => n.Instructor.Name); // Descending
            }
            else if (CourseStartDateSortOrder != null && CourseStartDateSortOrder == 2)
            {
                clientFilter = CourseStartDateSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.StartDate) // Ascending
                                   : clientFilter.OrderByDescending(n => n.StartDate); // Descending
            }
            else if (CourseEndDateSortOrder != null && CourseEndDateSortOrder == 2)
            {
                clientFilter = CourseEndDateSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.EndDate) // Ascending
                                   : clientFilter.OrderByDescending(n => n.EndDate); // Descending
            }
            else if (ClassSizeSortOrder != null && ClassSizeSortOrder == 2)
            {
                clientFilter = ClassSizeSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.ClassSize) // Ascending
                                   : clientFilter.OrderByDescending(n => n.ClassSize); // Descending
            }

            if (CourseNameSortOrder != null && CourseNameSortOrder == 3)
            {
                clientFilter = CourseNameSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.Title) // Ascending
                                   : clientFilter.OrderByDescending(n => n.Title); // Descending
            }
            else if (InstructorNameSortOrder != null && InstructorNameSortOrder == 3)
            {
                clientFilter = InstructorNameSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.Instructor.Name) // Ascending
                                   : clientFilter.OrderByDescending(n => n.Instructor.Name); // Descending
            }
            else if (CourseStartDateSortOrder != null && CourseStartDateSortOrder == 3)
            {
                clientFilter = CourseStartDateSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.StartDate) // Ascending
                                   : clientFilter.OrderByDescending(n => n.StartDate); // Descending
            }
            else if (CourseEndDateSortOrder != null && CourseEndDateSortOrder == 3)
            {
                clientFilter = CourseEndDateSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.EndDate) // Ascending
                                   : clientFilter.OrderByDescending(n => n.EndDate); // Descending
            }
            else if (ClassSizeSortOrder != null && ClassSizeSortOrder == 3)
            {
                clientFilter = ClassSizeSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.ClassSize) // Ascending
                                   : clientFilter.OrderByDescending(n => n.ClassSize); // Descending
            }

            if (CourseNameSortOrder != null && CourseNameSortOrder == 4)
            {
                clientFilter = CourseNameSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.Title) // Ascending
                                   : clientFilter.OrderByDescending(n => n.Title); // Descending
            }
            else if (InstructorNameSortOrder != null && InstructorNameSortOrder == 4)
            {
                clientFilter = InstructorNameSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.Instructor.Name) // Ascending
                                   : clientFilter.OrderByDescending(n => n.Instructor.Name); // Descending
            }
            else if (CourseStartDateSortOrder != null && CourseStartDateSortOrder == 4)
            {
                clientFilter = CourseStartDateSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.StartDate) // Ascending
                                   : clientFilter.OrderByDescending(n => n.StartDate); // Descending
            }
            else if (CourseEndDateSortOrder != null && CourseEndDateSortOrder == 4)
            {
                clientFilter = CourseEndDateSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.EndDate) // Ascending
                                   : clientFilter.OrderByDescending(n => n.EndDate); // Descending
            }
            else if (ClassSizeSortOrder != null && ClassSizeSortOrder == 4)
            {
                clientFilter = ClassSizeSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.ClassSize) // Ascending
                                   : clientFilter.OrderByDescending(n => n.ClassSize); // Descending
            }

            if (CourseNameSortOrder != null && CourseNameSortOrder == 5)
            {
                clientFilter = CourseNameSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.Title) // Ascending
                                   : clientFilter.OrderByDescending(n => n.Title); // Descending
            }
            else if (InstructorNameSortOrder != null && InstructorNameSortOrder == 5)
            {
                clientFilter = InstructorNameSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.Instructor.Name) // Ascending
                                   : clientFilter.OrderByDescending(n => n.Instructor.Name); // Descending
            }
            else if (CourseStartDateSortOrder != null && CourseStartDateSortOrder == 5)
            {
                clientFilter = CourseStartDateSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.StartDate) // Ascending
                                   : clientFilter.OrderByDescending(n => n.StartDate); // Descending
            }
            else if (CourseEndDateSortOrder != null && CourseEndDateSortOrder == 5)
            {
                clientFilter = CourseEndDateSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.EndDate) // Ascending
                                   : clientFilter.OrderByDescending(n => n.EndDate); // Descending
            }
            else if (ClassSizeSortOrder != null && ClassSizeSortOrder == 5)
            {
                clientFilter = ClassSizeSortDirectionIndex == 1
                                   ? clientFilter.OrderBy(n => n.ClassSize) // Ascending
                                   : clientFilter.OrderByDescending(n => n.ClassSize); // Descending
            }

            // the default sort order is CourseId
            if (clientFilter.SortExpressions.Count == 0)
            {
                clientFilter = clientFilter.OrderBy(n => n.CourseId);
            }
            return clientFilter;
        }

        #endregion "Private Methods"
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

Comments and Discussions