Filter Collection by Multiple Criteria
Clash Royale CLAN TAG#URR8PPP
I want to the user to filter a list of receipts by various criteria. Just the regular, an empty filter should show all items, an entry in customer should show all receipts from that customer and an additional entry in date should show all entries from said customer on the given date. I have the feeling, my if else apprach is not the best way, since with 4 criteria I'm already at 16 branches, not speak of 5, 6, or 7 criteria.
What is the most elegant way to achieve this.
Model:
class customer
public int ID get; set;
public string Name get; set;
class receipt
public int ID get; set;
public string Number get; set;
public DateTime Date get; set;
public double Amount get; set;
public customer Customer get; set;
Viewmodel
class receiptViewModel
{
ObservableCollection<receipt> ReceiptList get; set;
List<receipt> ReceiptListView get; set;
private string filter;
public string Filter
get return filter;
set
filter = value;
if (number != null && date == null && customer && null)
ReceiptListView = ReceiptList.Where(x => x.Number.Contains(number)).ToList();
else if (number != null && date != null && customer && null)
ReceiptListView = ReceiptList.Where(x => x.Number.Contains(number) && x.Date === date).ToList();
//aso aso aso
c# mvvm
add a comment |
I want to the user to filter a list of receipts by various criteria. Just the regular, an empty filter should show all items, an entry in customer should show all receipts from that customer and an additional entry in date should show all entries from said customer on the given date. I have the feeling, my if else apprach is not the best way, since with 4 criteria I'm already at 16 branches, not speak of 5, 6, or 7 criteria.
What is the most elegant way to achieve this.
Model:
class customer
public int ID get; set;
public string Name get; set;
class receipt
public int ID get; set;
public string Number get; set;
public DateTime Date get; set;
public double Amount get; set;
public customer Customer get; set;
Viewmodel
class receiptViewModel
{
ObservableCollection<receipt> ReceiptList get; set;
List<receipt> ReceiptListView get; set;
private string filter;
public string Filter
get return filter;
set
filter = value;
if (number != null && date == null && customer && null)
ReceiptListView = ReceiptList.Where(x => x.Number.Contains(number)).ToList();
else if (number != null && date != null && customer && null)
ReceiptListView = ReceiptList.Where(x => x.Number.Contains(number) && x.Date === date).ToList();
//aso aso aso
c# mvvm
add a comment |
I want to the user to filter a list of receipts by various criteria. Just the regular, an empty filter should show all items, an entry in customer should show all receipts from that customer and an additional entry in date should show all entries from said customer on the given date. I have the feeling, my if else apprach is not the best way, since with 4 criteria I'm already at 16 branches, not speak of 5, 6, or 7 criteria.
What is the most elegant way to achieve this.
Model:
class customer
public int ID get; set;
public string Name get; set;
class receipt
public int ID get; set;
public string Number get; set;
public DateTime Date get; set;
public double Amount get; set;
public customer Customer get; set;
Viewmodel
class receiptViewModel
{
ObservableCollection<receipt> ReceiptList get; set;
List<receipt> ReceiptListView get; set;
private string filter;
public string Filter
get return filter;
set
filter = value;
if (number != null && date == null && customer && null)
ReceiptListView = ReceiptList.Where(x => x.Number.Contains(number)).ToList();
else if (number != null && date != null && customer && null)
ReceiptListView = ReceiptList.Where(x => x.Number.Contains(number) && x.Date === date).ToList();
//aso aso aso
c# mvvm
I want to the user to filter a list of receipts by various criteria. Just the regular, an empty filter should show all items, an entry in customer should show all receipts from that customer and an additional entry in date should show all entries from said customer on the given date. I have the feeling, my if else apprach is not the best way, since with 4 criteria I'm already at 16 branches, not speak of 5, 6, or 7 criteria.
What is the most elegant way to achieve this.
Model:
class customer
public int ID get; set;
public string Name get; set;
class receipt
public int ID get; set;
public string Number get; set;
public DateTime Date get; set;
public double Amount get; set;
public customer Customer get; set;
Viewmodel
class receiptViewModel
{
ObservableCollection<receipt> ReceiptList get; set;
List<receipt> ReceiptListView get; set;
private string filter;
public string Filter
get return filter;
set
filter = value;
if (number != null && date == null && customer && null)
ReceiptListView = ReceiptList.Where(x => x.Number.Contains(number)).ToList();
else if (number != null && date != null && customer && null)
ReceiptListView = ReceiptList.Where(x => x.Number.Contains(number) && x.Date === date).ToList();
//aso aso aso
c# mvvm
c# mvvm
asked Dec 25 '18 at 15:20
Mister 832
1905
1905
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can build the LINQ query in several steps by appending new where clauses
IEnumerable<receipt> query = ReceiptList;
if (customer != null)
query = query.Where(x => x.CustomerId == customer.ID);
if (number != null)
query = query.Where(x => x.Number.Contains(number));
if (date != null)
query = query.Where(x => x.Date == date);
...
ReceiptListView = query.ToList();
This reduces the complexity from O(2ⁿ)
to O(n)
2
It's probably also worth to look into linqkit's predicatebuilder if more complex predicates have to be put together (say instead of combining all the limitations, wanting "or").
– Voo
Dec 26 '18 at 0:00
Very nice, thank you!
– Mister 832
Dec 26 '18 at 8:41
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210313%2ffilter-collection-by-multiple-criteria%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can build the LINQ query in several steps by appending new where clauses
IEnumerable<receipt> query = ReceiptList;
if (customer != null)
query = query.Where(x => x.CustomerId == customer.ID);
if (number != null)
query = query.Where(x => x.Number.Contains(number));
if (date != null)
query = query.Where(x => x.Date == date);
...
ReceiptListView = query.ToList();
This reduces the complexity from O(2ⁿ)
to O(n)
2
It's probably also worth to look into linqkit's predicatebuilder if more complex predicates have to be put together (say instead of combining all the limitations, wanting "or").
– Voo
Dec 26 '18 at 0:00
Very nice, thank you!
– Mister 832
Dec 26 '18 at 8:41
add a comment |
You can build the LINQ query in several steps by appending new where clauses
IEnumerable<receipt> query = ReceiptList;
if (customer != null)
query = query.Where(x => x.CustomerId == customer.ID);
if (number != null)
query = query.Where(x => x.Number.Contains(number));
if (date != null)
query = query.Where(x => x.Date == date);
...
ReceiptListView = query.ToList();
This reduces the complexity from O(2ⁿ)
to O(n)
2
It's probably also worth to look into linqkit's predicatebuilder if more complex predicates have to be put together (say instead of combining all the limitations, wanting "or").
– Voo
Dec 26 '18 at 0:00
Very nice, thank you!
– Mister 832
Dec 26 '18 at 8:41
add a comment |
You can build the LINQ query in several steps by appending new where clauses
IEnumerable<receipt> query = ReceiptList;
if (customer != null)
query = query.Where(x => x.CustomerId == customer.ID);
if (number != null)
query = query.Where(x => x.Number.Contains(number));
if (date != null)
query = query.Where(x => x.Date == date);
...
ReceiptListView = query.ToList();
This reduces the complexity from O(2ⁿ)
to O(n)
You can build the LINQ query in several steps by appending new where clauses
IEnumerable<receipt> query = ReceiptList;
if (customer != null)
query = query.Where(x => x.CustomerId == customer.ID);
if (number != null)
query = query.Where(x => x.Number.Contains(number));
if (date != null)
query = query.Where(x => x.Date == date);
...
ReceiptListView = query.ToList();
This reduces the complexity from O(2ⁿ)
to O(n)
edited Dec 25 '18 at 18:39
answered Dec 25 '18 at 15:29
Olivier Jacot-Descombes
2,5981117
2,5981117
2
It's probably also worth to look into linqkit's predicatebuilder if more complex predicates have to be put together (say instead of combining all the limitations, wanting "or").
– Voo
Dec 26 '18 at 0:00
Very nice, thank you!
– Mister 832
Dec 26 '18 at 8:41
add a comment |
2
It's probably also worth to look into linqkit's predicatebuilder if more complex predicates have to be put together (say instead of combining all the limitations, wanting "or").
– Voo
Dec 26 '18 at 0:00
Very nice, thank you!
– Mister 832
Dec 26 '18 at 8:41
2
2
It's probably also worth to look into linqkit's predicatebuilder if more complex predicates have to be put together (say instead of combining all the limitations, wanting "or").
– Voo
Dec 26 '18 at 0:00
It's probably also worth to look into linqkit's predicatebuilder if more complex predicates have to be put together (say instead of combining all the limitations, wanting "or").
– Voo
Dec 26 '18 at 0:00
Very nice, thank you!
– Mister 832
Dec 26 '18 at 8:41
Very nice, thank you!
– Mister 832
Dec 26 '18 at 8:41
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210313%2ffilter-collection-by-multiple-criteria%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown