Filter Collection by Multiple Criteria

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












11














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


























    11














    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
























      11












      11








      11


      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 Dec 25 '18 at 15:20









      Mister 832

      1905




      1905




















          1 Answer
          1






          active

          oldest

          votes


















          11














          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
            Dec 26 '18 at 0:00










          • Very nice, thank you!
            – Mister 832
            Dec 26 '18 at 8:41










          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









          11














          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
            Dec 26 '18 at 0:00










          • Very nice, thank you!
            – Mister 832
            Dec 26 '18 at 8:41















          11














          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
            Dec 26 '18 at 0:00










          • Very nice, thank you!
            – Mister 832
            Dec 26 '18 at 8:41













          11












          11








          11






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












          • 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

















          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

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay