12
\$\begingroup\$

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
 }
 }
asked Dec 25, 2018 at 15:20
\$\endgroup\$

1 Answer 1

13
\$\begingroup\$

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(2n) to O(n)

answered Dec 25, 2018 at 15:29
\$\endgroup\$
1
  • 2
    \$\begingroup\$ 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"). \$\endgroup\$ Commented Dec 26, 2018 at 0: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.