Click here to Skip to main content
15,885,910 members
Articles / Desktop Programming / WPF

Dynamic LINQ to Entities Queries Using WCF/WPF Demo Code

Rate me:
Please Sign up or sign in to vote.
4.93/5 (86 votes)
30 Nov 2008CPOL22 min read 249.1K   4.5K   230  
Demonstrates a method of dynamic query across WCF Service boundaries.
using System.Windows;
using System.Windows.Controls;
using System;
using WpfClient.Commands;
using System.Collections.Generic;


namespace WpfClient.UserControls
{

    public delegate void SearchRequestedEventHandler(object sender, SearchEventArgs e);


    /// <summary>
    /// Interaction logic for SearchControl.xaml
    /// </summary>
    public partial class SearchControl : UserControl
    {

        #region Data
        private RelayCommand addSearchCommand;
        private RelayCommand runSearch;
        #endregion

        #region Ctor
        public SearchControl()
        {
            InitializeComponent();
            this.DataContext = this;
        }
        #endregion

        #region BoundType DP

        #region BoundType

        /// <summary>
        /// IsAvailable Dependency Property
        /// </summary>
        public static readonly DependencyProperty BoundTypeProperty =
            DependencyProperty.Register("BoundType", 
                typeof(System.Type), typeof(SearchControl),
                    new FrameworkPropertyMetadata(null,
                        new PropertyChangedCallback(OnBoundTypeChanged)));

        /// <summary>
        /// Gets or sets the BoundCustomer property.  
        /// </summary>
        public Type BoundType
        {
            get { return (Type)GetValue(BoundTypeProperty); }
            set { SetValue(BoundTypeProperty, value); }
        }

        /// <summary>
        /// Handles changes to the BoundType property.
        /// </summary>
        private static void OnBoundTypeChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            Type currentObject = e.NewValue.GetType();

            if (currentObject != null)
            {
                SearchControl searchControl = (SearchControl)d;
                foreach (SearchClauseControl searchClause in searchControl.spSearchClauses.Children)
                {
                    searchClause.BoundType = searchControl.BoundType;
                }
                searchControl.ReEstablishFirstClause();
            }
        }


        #endregion


        #endregion

        #region CLR Properties
        private String Query { get; set; }
        #endregion

        #region Routed Events
        public static readonly RoutedEvent SearchRequestedEvent =
            EventManager.RegisterRoutedEvent(
            "SearchRequested", RoutingStrategy.Bubble,
            typeof(SearchRequestedEventHandler),
            typeof(SearchControl));

        //add remove handlers
        public event SearchRequestedEventHandler SearchRequested
        {
            add { AddHandler(SearchRequestedEvent, value); }
            remove { RemoveHandler(SearchRequestedEvent, value); }
        }

        #endregion

        #region Commands
        public RelayCommand AddSearchCommand
        {
            get
            {
                if (addSearchCommand == null)
                {
                    addSearchCommand = new RelayCommand(
                        param => { return true;},
                        param =>
                            {
                                AddSearchClause();
                            }
                        );
                }
                return addSearchCommand;
            }
        }


        public RelayCommand RunSearchCommand
        {
            get
            {
                if (runSearch == null)
                {
                    runSearch = new RelayCommand(
                        param => { return true; },
                        param =>
                        {
                            RunSearch();
                        }
                        );
                }
                return runSearch;
            }
        }
        #endregion

        #region Public Methods
        public void Initialise()
        {
            spSearchClauses.Children.Clear();
        }

        #endregion

        #region Private Methods
        private void AddSearchClause()
        {
            SearchClauseControl searchClause = new SearchClauseControl();
            searchClause.BoundType = this.BoundType;
            searchClause.CloseSearchClause += searchClause_CloseSearchClause;
            spSearchClauses.Children.Add(searchClause);
            ReEstablishFirstClause();
        }

        private void searchClause_CloseSearchClause(object sender, System.Windows.RoutedEventArgs e)
        {
            spSearchClauses.Children.Remove(sender as SearchClauseControl);
            ReEstablishFirstClause();
        }

        private void ReEstablishFirstClause()
        {
            for (Int32 i = 0; i < spSearchClauses.Children.Count; i++)
            {
                (spSearchClauses.Children[i] as SearchClauseControl).IsFirst = i == 0 ? true : false;
                (spSearchClauses.Children[i] as SearchClauseControl).ParameterNumber = i;
            }
        }

        private void RunSearch()
        {
            Query = String.Empty;

            List<Object> searchParameters = new List<Object>();
            
            for (int i = 0; i < spSearchClauses.Children.Count; i++)
            {
                SearchClauseControl searchClauseControl = spSearchClauses.Children[i] as SearchClauseControl;
                //work out wording
                if (i == 0)
                    Query += searchClauseControl.ClauseResult;
                else
                    Query += " " + searchClauseControl.ClauseResult;

                //and store parameter value
                if (searchClauseControl.IsString)
                {
                    searchParameters.Add(searchClauseControl.CurrentValue);
                }
                else
                {
                    try
                    {
                        searchParameters.Add(Int32.Parse(searchClauseControl.CurrentValue));
                    }
                    catch (FormatException)
                    {
                        MessageBox.Show("Could convert one of the search parameter values");
                    }
                }


            }

            SearchEventArgs args =
                new SearchEventArgs(
                    SearchRequestedEvent,
                    searchParameters,
                    this.Query);
            RaiseEvent(args);
        }
        #endregion
    }
}

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 Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions