locked
best way to find single condition RRS feed

  • Question

  • hi all,

    i have 2 Lists.

    the first List has item with address, the second List has List of Addresses that should not be displayed.

    i added a condition that check if addresses is the same, but if the forbidden addresses has more than one item , the condition was not met.

    what can i do?

     foreach (var item in paymentsList)
                    {
                        foreach (string address in HidePlaces)
                        {
                            if (!item.Entity_Addresses.Contains(address))
                            {
                                //show item
                            }
                        }



    • Edited by liranppo Monday, August 3, 2020 5:41 AM
    Monday, August 3, 2020 5:38 AM

Answers

  • ok, i found the solution, thank you very much.

                    foreach (var item in paymentsList)
                    {
                        bool h = false;
                        foreach (string address in HidePlaces)
                        {
                            if (item.Entity_Addresses.Contains(address))
                            {
                                h = true;
                            }
                        }
                        if (!h)
                        {
                      //show item
                         }
    }


    • Marked as answer by liranppo Monday, August 3, 2020 6:56 AM
    • Edited by liranppo Monday, August 3, 2020 6:56 AM
    Monday, August 3, 2020 6:55 AM

All replies

  • Try something like this:

    foreach( var item in paymentsList)
    {
       bool h = false;
       foreach( string address in HidePlaces)
       {
          if( item.Entity_Addresses.Contains(address))
          {
             h = true;
             break;
          }
       }
       if( ! h )
       {
          . . . show item . . .
       }
    }

    You can also use LINQ and consider case-insensitive comparison.
    • Edited by Viorel_MVP Monday, August 3, 2020 6:18 AM
    Monday, August 3, 2020 6:17 AM
  • We can use continue to skip for the current loop:

    foreach (string address in HidePlaces)
    {
        if (!item.Entity_Addresses.Contains(address))
        {
           //show item
           break;
        }
     }


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, August 3, 2020 6:29 AM
  • thank you for the reply,

    both examples not working because if the address is on the first loop it does not show, but the next check with another address is show, even if it meets the condition the first time.

    Monday, August 3, 2020 6:46 AM
  • ok, i found the solution, thank you very much.

                    foreach (var item in paymentsList)
                    {
                        bool h = false;
                        foreach (string address in HidePlaces)
                        {
                            if (item.Entity_Addresses.Contains(address))
                            {
                                h = true;
                            }
                        }
                        if (!h)
                        {
                      //show item
                         }
    }


    • Marked as answer by liranppo Monday, August 3, 2020 6:56 AM
    • Edited by liranppo Monday, August 3, 2020 6:56 AM
    Monday, August 3, 2020 6:55 AM