Filter Collection by Multiple Criteria












9














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









share|improve this question



























    9














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









    share|improve this question

























      9












      9








      9


      1





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









      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 23 hours ago









      Mister 832

      1804




      1804






















          1 Answer
          1






          active

          oldest

          votes


















          9














          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)






          share|improve this answer



















          • 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
            14 hours ago










          • Very nice, thank you!
            – Mister 832
            6 hours ago











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


          }
          });














          draft saved

          draft discarded


















          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









          9














          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)






          share|improve this answer



















          • 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
            14 hours ago










          • Very nice, thank you!
            – Mister 832
            6 hours ago
















          9














          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)






          share|improve this answer



















          • 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
            14 hours ago










          • Very nice, thank you!
            – Mister 832
            6 hours ago














          9












          9








          9






          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)






          share|improve this answer














          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)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 20 hours ago

























          answered 23 hours ago









          Olivier Jacot-Descombes

          2,5781016




          2,5781016








          • 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
            14 hours ago










          • Very nice, thank you!
            – Mister 832
            6 hours ago














          • 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
            14 hours ago










          • Very nice, thank you!
            – Mister 832
            6 hours ago








          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
          14 hours ago




          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
          14 hours ago












          Very nice, thank you!
          – Mister 832
          6 hours ago




          Very nice, thank you!
          – Mister 832
          6 hours ago


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          How to ignore python UserWarning in pytest?

          Alexandru Averescu