Filtering DbContext data dynamically by user input in WPF applications

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite
1












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();










share|improve this question

















  • 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
















up vote
0
down vote

favorite
1












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();










share|improve this question

















  • 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












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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();










share|improve this question













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();












share|improve this question












share|improve this question




share|improve this question








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












  • 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










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.






share|improve this answer





















  • 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




    @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











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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.






share|improve this answer





















  • 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




    @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















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.






share|improve this answer





















  • 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




    @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













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.






share|improve this answer













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.







share|improve this answer













share|improve this answer



share|improve this answer











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 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




    @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






  • 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













 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?