none
Two list -- check some conditions -- looking for a good way RRS feed

  • Question

  • Hello,

    I have a int list.

    List<int> listIndexDefault;
     
    // Values from 1 to 10 or  30 to 78
     
    // I have a class Board 
    
    public class Board
    {
         string name;
         int Index;
    } 
    List<Board> ListBoard;

     
    A)
    Now I have to check, whether all values from the default list appear in the List <Board>? How can I solve this?
    B)
    The List <Board> must not contain additional indices that are not available in the default list. How can I solve this?
    C)
    In the List <Board>, the index from the default list may only appear once. How can I solve this?
    D)
    In the list <Board>, the index from the default list may appear multiple times. How can I solve this?


    Many regards for support with tips in advance.

       Markus


    Saturday, November 30, 2019 11:19 AM

Answers

  • Hi Markus,
     
    Here's a few ideas, nothing fancy. This is untested code, but there are no compile errors. I think it will work just fine:
      
    (You should make the index field in Board a public property for my examples below).

    A)
    int indexCount = 0;
    foreach (int index in listIndexDefault)
    {
        if (ListBoard.Exists(b => b.Index == index))
            indexCount++;
    }
    bool ContainsAllDefaults = indexCount == listIndexDefault.Count ? true : false;
    

         

    B)

    bool ContainsIndexNotInDefault = false;
    foreach (Board b in ListBoard)
    {
        if (listIndexDefault.Contains(b.Index))
            continue;
    
        ContainsIndexNotInDefault = true;
        break;
    }
    

    C) and D)

    int IndexFromDefaultList = listIndexDefault[0]; // or however you're choosing an Index
    indexCount = ListBoard.Count(b => b.Index == IndexFromDefaultList);
    // and obviously, indexCount would be = 1 for C) and >1 for D)


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Markus Freitag Tuesday, December 3, 2019 6:06 PM
    Saturday, November 30, 2019 10:14 PM
    Moderator

All replies

  • Hi Markus,
     
    Here's a few ideas, nothing fancy. This is untested code, but there are no compile errors. I think it will work just fine:
      
    (You should make the index field in Board a public property for my examples below).

    A)
    int indexCount = 0;
    foreach (int index in listIndexDefault)
    {
        if (ListBoard.Exists(b => b.Index == index))
            indexCount++;
    }
    bool ContainsAllDefaults = indexCount == listIndexDefault.Count ? true : false;
    

         

    B)

    bool ContainsIndexNotInDefault = false;
    foreach (Board b in ListBoard)
    {
        if (listIndexDefault.Contains(b.Index))
            continue;
    
        ContainsIndexNotInDefault = true;
        break;
    }
    

    C) and D)

    int IndexFromDefaultList = listIndexDefault[0]; // or however you're choosing an Index
    indexCount = ListBoard.Count(b => b.Index == IndexFromDefaultList);
    // and obviously, indexCount would be = 1 for C) and >1 for D)


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Markus Freitag Tuesday, December 3, 2019 6:06 PM
    Saturday, November 30, 2019 10:14 PM
    Moderator
  • Hi Bonnie, 

    Thanks and it’s work as expected. 

    Greetings Markus

    Tuesday, December 3, 2019 6:06 PM
  • You're welcome, Markus! Glad I could help!  =0)

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, December 8, 2019 9:33 PM
    Moderator