Filtering DbContext data dynamically by user input in WPF applications
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
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)
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();
c# database entity-framework wpf postgresql
add a comment |Â
up vote
0
down vote
favorite
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)
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();
c# database entity-framework wpf postgresql
1
What's with the code that is commented out?
â t3chb0t
Apr 16 at 6:38
@t3chb0t, they were just examples of possible filtering elements (filter by email string containing .., has category x, etc..)
â W.M.
Apr 16 at 15:40
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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)
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();
c# database entity-framework wpf postgresql
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)
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();
c# database entity-framework wpf postgresql
edited Apr 16 at 6:37
t3chb0t
32k54195
32k54195
asked Apr 15 at 19:59
W.M.
1032
1032
1
What's with the code that is commented out?
â t3chb0t
Apr 16 at 6:38
@t3chb0t, they were just examples of possible filtering elements (filter by email string containing .., has category x, etc..)
â W.M.
Apr 16 at 15:40
add a comment |Â
1
What's with the code that is commented out?
â t3chb0t
Apr 16 at 6:38
@t3chb0t, they were just examples of possible filtering elements (filter by email string containing .., has category x, etc..)
â W.M.
Apr 16 at 15:40
1
1
What's with the code that is commented out?
â t3chb0t
Apr 16 at 6:38
What's with the code that is commented out?
â t3chb0t
Apr 16 at 6:38
@t3chb0t, they were just examples of possible filtering elements (filter by email string containing .., has category x, etc..)
â W.M.
Apr 16 at 15:40
@t3chb0t, they were just examples of possible filtering elements (filter by email string containing .., has category x, etc..)
â W.M.
Apr 16 at 15:40
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
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)
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.
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 tovar items = db.clients.AsQueryable(); items = FilterClients(db, items); dataGrid.ItemsSource = items.ToList();
, does this involve calling the database twice or once (I mean because ofdb.clients.AsQueryable()
and then in next lineFilterClients(db, items)
)?
â W.M.
Apr 16 at 15:46
1
@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.
â Flater
Apr 16 at 17:00
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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)
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.
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 tovar items = db.clients.AsQueryable(); items = FilterClients(db, items); dataGrid.ItemsSource = items.ToList();
, does this involve calling the database twice or once (I mean because ofdb.clients.AsQueryable()
and then in next lineFilterClients(db, items)
)?
â W.M.
Apr 16 at 15:46
1
@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.
â Flater
Apr 16 at 17:00
add a comment |Â
up vote
2
down vote
accepted
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)
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.
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 tovar items = db.clients.AsQueryable(); items = FilterClients(db, items); dataGrid.ItemsSource = items.ToList();
, does this involve calling the database twice or once (I mean because ofdb.clients.AsQueryable()
and then in next lineFilterClients(db, items)
)?
â W.M.
Apr 16 at 15:46
1
@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.
â Flater
Apr 16 at 17:00
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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)
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.
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)
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 at 13:33
Flater
2,645718
2,645718
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 tovar items = db.clients.AsQueryable(); items = FilterClients(db, items); dataGrid.ItemsSource = items.ToList();
, does this involve calling the database twice or once (I mean because ofdb.clients.AsQueryable()
and then in next lineFilterClients(db, items)
)?
â W.M.
Apr 16 at 15:46
1
@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.
â Flater
Apr 16 at 17:00
add a comment |Â
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 tovar items = db.clients.AsQueryable(); items = FilterClients(db, items); dataGrid.ItemsSource = items.ToList();
, does this involve calling the database twice or once (I mean because ofdb.clients.AsQueryable()
and then in next lineFilterClients(db, items)
)?
â W.M.
Apr 16 at 15:46
1
@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.
â Flater
Apr 16 at 17:00
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)
)?â W.M.
Apr 16 at 15:46
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)
)?â W.M.
Apr 16 at 15:46
1
1
@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.â Flater
Apr 16 at 17:00
@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.â Flater
Apr 16 at 17:00
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192141%2ffiltering-dbcontext-data-dynamically-by-user-input-in-wpf-applications%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
What's with the code that is commented out?
â t3chb0t
Apr 16 at 6:38
@t3chb0t, they were just examples of possible filtering elements (filter by email string containing .., has category x, etc..)
â W.M.
Apr 16 at 15:40