Filtering with WPF, ADO.NET Entity Data Model

For this example I am using the Northwind database that comes with SQL Server 2008 Express.

For this example I am using the Employees table.

I am only using the following 5 columns of the Employees table

EmployeeID, LastName, FirstName, Title, PhotoPath


I currently have 2 files in my WPF application.

The page with the XAML is called:  FilterByLastAndFirst.xaml

The code behind page  is called:  FilterByLastAndFirst.xaml.cs

When I created my WPF application using Visual Studio 2008 I then added a data source, I used the ADO.NET Entity Data Model.

I set up a filter to filter my table my Last Name. So I type in the last name and click the filter button and it filters by last name.

My code shown works fine and allows me to filter by last name, but now I want to be able to filter by 2 parameters. LastName and FirstName.

Does anyone know how to modify my existing code behind to allow to filter by both LastName and FirstName.

In my xaml page I added the textbox colled: txtFirstName

But I stuck on tweaking the code behind to add this second parameter.

I have attached my currently working code behind file. This currenly only allows me to filter by last name. I want to wire the texbox called txtFirstName as a second parameter.


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 Filters.Data;


namespace Filters
{
    /// <summary>
    /// Interaction logic for FilterByLastAndFirst.xaml
    /// </summary>
    public partial class FilterByLastAndFirst : Window
    {
         public FilterByLastAndFirst()
        {
            InitializeComponent();

            FillData();
        }

        /// <summary>
        /// Purpose:  Fill the list box with data
        /// </summary>
        private void FillData()
        {
            NorthwindEntities nw = new NorthwindEntities();

            if (txtLastName.Text.Length < 1)
            {
                var q = from emp in nw.Employees
                        select emp;
                lstEmployees.ItemsSource = q.ToList();
            }
            else
            {
                string lname = Convert.ToString(txtLastName.Text);

                var q = from emp in nw.Employees
                        where emp.LastName == lname
                        select emp;

                lstEmployees.ItemsSource = q.ToList();
            }


        }

        private void btnFilter_Click(object sender, RoutedEventArgs e)
        {
            FillData();
        }
    }
}

Open in new window

screen1.jpg
screen2.jpg
LVL 1
maqskywalkerAsked:
Who is Participating?
 
lenordisteCommented:
another cleaner solution could be:

IQueryable<Employee> query = from emp  in nw.Employees
 select emp;

//initial result
query = from b in query
select b;
//lastname is provided
if (!String.IsNullOrEmpty(txtLastName.Text))
{
query = query.where(emp => emp.LastName==txtLastName.Text);
}
//lastname is provided
if (!String.IsNullOrEmpty(txtFirstName.Text))
{
query = query.where(emp => emp.FirstName==txtFirstName.Text);
}
 lstEmployees.ItemsSource = query.ToList();


==>Remember that Linq will only query when calling the ToList() method :)
and a correction to my previous solution where I accidently inversed values (don't have VS2008 on this comp to check):
var q = from emp in nw.Employees
                        where
                           (txtLastName.Text.Length < 1? true:emp.LastName==txtLastName.Text) &&
                           (txtFirstName.Text.Length < 1? true:emp.FirstName==txtFirstName.Text)
                        select emp;
0
 
Grant SpiteriSenior consultantCommented:
Wouldnt you simply extend your filldata method with the query

var q = from emp in nw.Employees
                        where emp.LastName == lname and emp.FirstName == fname
                        select emp;

to continue with the same pattern to me personally that actually looks more like an exact search then a filter
0
 
lenordisteCommented:
it's a bit of a hack but this typically works fine:

var q = from emp in nw.Employees
                        where
                           (txtLastName.Text.Length < 1? emp.LastName==txtLastName.Text.Length < 1:true) &&
                           (txtFirstName.Text.Length < 1? emp.FirstName==txtFirstName.Text.Length < 1:true)
                        select emp;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.