Click here to Skip to main content
15,891,607 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 250K   4.5K   230  
Demonstrates a method of dynamic query across WCF Service boundaries.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfClient.ServiceProxy;
using System.ServiceModel;
using WpfClient.ServiceReference;

using FluidKit.Controls;
using System.Diagnostics;
using WpfClient.Commands;




namespace WpfClient
{
    /// <summary>
    /// Contains a view of some Northwind Customers within
    /// Pavan Podilas excellent ElementFlow. Also show
    /// associated Orders for the selected Customer
    /// </summary>
    public partial class CustomerOrdersWindow : Window
    {
        #region Data
        private CustomerOrdersViewModel viewModel = new CustomerOrdersViewModel();
        private RelayCommand searchCommand;
        private RelayCommand diagCommand;
        private ElementFlow elementFlow;
        private int viewIndex;
        private ViewStateBase[] views = {
		                                 	new CoverFlow(),
		                                 	new Carousel(),
		                                 	new RollerCoaster(),
		                                 	new Rolodex(),
		                                 };
        #endregion

        #region Ctor
        public CustomerOrdersWindow()
        {
            InitializeComponent();
            this.DataContext = viewModel;
            this.Loaded += Window1_Loaded;
            this.btnSearch.Command = this.SearchCommand;
            this.btnDiagnostics.Command = this.DiagCommand;
        }
        #endregion

        #region Commands
        public RelayCommand SearchCommand
        {
            get
            {
                if (searchCommand == null)
                {
                    searchCommand = new RelayCommand(
                        param => { return true; },
                        param =>
                        {
                            DoSearch();
                        }
                        );
                }
                return searchCommand;
            }
        }


        public RelayCommand DiagCommand
        {
            get
            {
                if (diagCommand == null)
                {
                    diagCommand = new RelayCommand(
                        param => { return true; },
                        param =>
                        {
                            DoDiagnostics();
                        }
                        );
                }
                return diagCommand;
            }
        }
        #endregion

        #region Private Methods
        private void ElementFlowSetup()
        {
            // Get reference to ElementFlow
            DependencyObject obj = VisualTreeHelper.GetChild(lstCustomers, 0);
            while ((obj is ElementFlow) == false)
            {
                obj = VisualTreeHelper.GetChild(obj, 0);
            }
            elementFlow = obj as ElementFlow;

            elementFlow.SelectedIndexChanged += EFSelectedIndexChanged;
            elementFlow.SelectedIndex = 0;
        }

        private void Window1_Loaded(object sender, RoutedEventArgs e)
        {

            ElementFlowSetup();
            viewModel.FetchDefaultCustomerWithOrders();
            custControl.BoundCustomer = viewModel.SelectedCustomer;
            custOrdersControl.BoundCustomer = viewModel.SelectedCustomer;
            searchControl.BoundType = typeof (Customers);
        }


        private void EFSelectedIndexChanged(object sender, EventArgs e)
        {
            if (viewModel != null && viewModel.CurrentCustomers.Count > 0)
            {
                try
                {
                    //Get selected customer to show from ElementFlow
                    viewModel.SelectedCustomer =
                        viewModel.CurrentCustomers[(sender as ElementFlow).SelectedIndex];
                    custControl.BoundCustomer = viewModel.SelectedCustomer;
                    custOrdersControl.BoundCustomer = viewModel.SelectedCustomer;
                }
                catch { }
            }
        }


        private void DoSearch()
        {
            searchControl.Visibility = Visibility.Visible;
            searchControl.Initialise();
            searchControl.SearchRequested += searchControl_SearchRequested;    
        }

        private void searchControl_SearchRequested(object sender, SearchEventArgs e)
        {
            if (searchControl.Visibility == Visibility.Visible)
            {
                searchControl.Visibility = Visibility.Hidden;
                viewModel.SearchCustomerWithOrders(e.WhereClause, e.SearchParameters);
            }
        }

  

        private void DoDiagnostics()
        {
            DiagnosticsWindow diagWin = new DiagnosticsWindow();
            diagWin.ShowInTaskbar = false;
            diagWin.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            diagWin.Owner = this;
            diagWin.ShowDialog();
        }
        #endregion

        #region Overrides
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.F12)
            {
                viewIndex = (viewIndex + 1) % views.Length;
                elementFlow.CurrentView = views[viewIndex];
            }
        }
        #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