0
\$\begingroup\$

I need to filter database data based on filters available to the end user in the form of search term text box, select boxes etc.

I have put together this code and need feedback if this is a good way to do it or if there are any better solutions.

using Multi.Model;
using System;
using System.Linq;
using System.Windows.Controls;
namespace Multi.Pages
{
 /// <summary>
 /// Interaction logic for Page1.xaml
 /// </summary>
 public partial class Page1 : Page
 {
 public Page1()
 {
 InitializeComponent();
 textBoxName.TextChanged += TextBoxName_TextChanged;
 PopulateDataGrid();
 }
 private void PopulateDataGrid()
 {
 using (var db = new optisysEntities())
 {
 var items = db.clients.AsQueryable();
 items = FilterClients(db, items);
 dataGrid.ItemsSource = items.ToList();
 }
 }
 private System.Linq.IQueryable<Multi.Model.clients> FilterClients(optisysEntities db, System.Linq.IQueryable<Multi.Model.clients> clients)
 {
 if (!String.IsNullOrWhiteSpace(textBoxName.Text)) clients = db.clients.Where(c => c.name.Contains(textBoxName.Text)
 || c.phone.Contains(textBoxName.Text));
 // if (!String.IsNullOrWhiteSpace(search.Email)) clients = clients.Where(u => u.Email.Contains(search.Email));
 // if (search.UsertypeId.HasValue) clients = clients.Where(u => u.UsertypeId == search.UsertypeId.Value);
 return clients;
 }
 private void TextBoxName_TextChanged(object sender, TextChangedEventArgs e)
 {
 PopulateDataGrid();
 }
 }
}
t3chb0t
44.6k9 gold badges84 silver badges190 bronze badges
asked Apr 15, 2018 at 19:59
\$\endgroup\$
2
  • 1
    \$\begingroup\$ What's with the code that is commented out? \$\endgroup\$ Commented Apr 16, 2018 at 6:38
  • \$\begingroup\$ @t3chb0t, they were just examples of possible filtering elements (filter by email string containing .., has category x, etc..) \$\endgroup\$ Commented Apr 16, 2018 at 15:40

1 Answer 1

2
\$\begingroup\$

Reducing your code to what I think is the core of your question:

private System.Linq.IQueryable<Multi.Model.clients> FilterClients(optisysEntities db, System.Linq.IQueryable<Multi.Model.clients> clients)
{
 if (!String.IsNullOrWhiteSpace(textBoxName.Text)) 
 clients = db.clients.Where(c => c.name.Contains(textBoxName.Text) || c.phone.Contains(textBoxName.Text));
 if (!String.IsNullOrWhiteSpace(search.Email)) 
 clients = clients.Where(u => u.Email.Contains(search.Email));
 if (search.UsertypeId.HasValue) 
 clients = clients.Where(u => u.UsertypeId == search.UsertypeId.Value);
 return clients;
}

Yes, this is one of the better ways to do dynamic filtering.


I would suggest abstracting the data retrieval into a separate layer. You don't want your form logic handling your underlying ORM directly.

This is exacerbated by the fact that you've put the FilterClients() method by itself. Your UI form therefore contains a method whose reponsibility has nothing to do with the UI.

However, I get the feeling that this application is either tiny, or has only just been developed. So I can understand that this abstraction is something for a later stage. I would suggest doing it immediately to make it less painful in the future, but that's your choice.

answered Apr 16, 2018 at 13:33
\$\endgroup\$
2
  • \$\begingroup\$ Thank you very much @Flater for the valuable information. I wrote that mostly for learning about EF and WPF. I am not sure if you had paid attention to var items = db.clients.AsQueryable(); items = FilterClients(db, items); dataGrid.ItemsSource = items.ToList();, does this involve calling the database twice or once (I mean because of db.clients.AsQueryable() and then in next line FilterClients(db, items))? \$\endgroup\$ Commented Apr 16, 2018 at 15:46
  • 1
    \$\begingroup\$ @W.M. As long as it stays an IQueryable object, and you don't access the data (e.g. look at one of the items on the list, or count how many there are in the list), your code should not yet connect to the database. ToList, however, enumerates the data which does involve fetching it from the db. There are a lot of ins and outs as to when data is instantiated, this comment is giving you the simple rundown. \$\endgroup\$ Commented Apr 16, 2018 at 17:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.