Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to check items in a Collection contains common property?

How to check items in a Collection contains common property?

Scheduled Pinned Locked Moved C#
tutorialcsharpquestion
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John ph
    wrote on last edited by
    #1

    Is there any better way in c# to check items in a collection contains common property besides looping through with a flag and finding out? say for example, a text item collection contains fontweight bold for all text item

    - Regards -
       J O N


    A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers


    OriginalGriffO P 2 Replies Last reply
    0
    • J John ph

      Is there any better way in c# to check items in a collection contains common property besides looping through with a flag and finding out? say for example, a text item collection contains fontweight bold for all text item

      - Regards -
         J O N


      A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers


      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Not really. You may be able to do it with Linq, but how to do that will depend on the items you are talking about.

      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      J 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Not really. You may be able to do it with Linq, but how to do that will depend on the items you are talking about.

        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

        J Offline
        J Offline
        John ph
        wrote on last edited by
        #3

        trying to do something like this

        textBold.Checked = true;
        foreach (Item item in Items)
        {
        if (item.FontWeight == FontWeights.Normal)
        {
        textBold.Checked = false;
        }
        }

        but FontWeights include more values

        - Regards -
           J O N


        A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers


        OriginalGriffO 1 Reply Last reply
        0
        • J John ph

          trying to do something like this

          textBold.Checked = true;
          foreach (Item item in Items)
          {
          if (item.FontWeight == FontWeights.Normal)
          {
          textBold.Checked = false;
          }
          }

          but FontWeights include more values

          - Regards -
             J O N


          A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers


          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Add a break: textBold.Checked = true;

          foreach (Item item in Items)
          {
          if (item.FontWeight == FontWeights.Normal)
          {
          textBold.Checked = false;
          break;
          }
          }

          As I said, you could do it with Linq:

                  int normal = Items.Count(item => item.FontWeight == FontWeights.Normal);
          

          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          J 1 Reply Last reply
          0
          • J John ph

            Is there any better way in c# to check items in a collection contains common property besides looping through with a flag and finding out? say for example, a text item collection contains fontweight bold for all text item

            - Regards -
               J O N


            A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers


            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            Now the solution doesn't make much sense because you've removed a link in the chain. Other people might have found this useful, but you've made it hard for them. Never, ever, remove a message that's been replied to.

            I'm not a stalker, I just know things. Oh by the way, you're out of milk.

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Onyx

            L J 2 Replies Last reply
            0
            • P Pete OHanlon

              Now the solution doesn't make much sense because you've removed a link in the chain. Other people might have found this useful, but you've made it hard for them. Never, ever, remove a message that's been replied to.

              I'm not a stalker, I just know things. Oh by the way, you're out of milk.

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Onyx

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              I agree, however IMO you're asking the wrong person. It is Chris who simply shouldn't present a "Delete" widget for messages that have replies. I have asked him many times, he eventually fixed half of it (the top level message). :)

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              P OriginalGriffO 2 Replies Last reply
              0
              • L Luc Pattyn

                I agree, however IMO you're asking the wrong person. It is Chris who simply shouldn't present a "Delete" widget for messages that have replies. I have asked him many times, he eventually fixed half of it (the top level message). :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                I know. I was going to post a request and then thought, what's the point - it's been asked for before and still isn't here.

                I'm not a stalker, I just know things. Oh by the way, you're out of milk.

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Onyx

                1 Reply Last reply
                0
                • P Pete OHanlon

                  Now the solution doesn't make much sense because you've removed a link in the chain. Other people might have found this useful, but you've made it hard for them. Never, ever, remove a message that's been replied to.

                  I'm not a stalker, I just know things. Oh by the way, you're out of milk.

                  Forgive your enemies - it messes with their heads

                  My blog | My articles | MoXAML PowerToys | Onyx

                  J Offline
                  J Offline
                  John ph
                  wrote on last edited by
                  #8

                  sorry, not intended to remove. it was a mistake.

                  - Regards -
                     J O N


                  A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers


                  1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Add a break: textBold.Checked = true;

                    foreach (Item item in Items)
                    {
                    if (item.FontWeight == FontWeights.Normal)
                    {
                    textBold.Checked = false;
                    break;
                    }
                    }

                    As I said, you could do it with Linq:

                            int normal = Items.Count(item => item.FontWeight == FontWeights.Normal);
                    

                    Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                    J Offline
                    J Offline
                    John ph
                    wrote on last edited by
                    #9

                    thanks a lot. implemented by storing the item properties in a list and list.contains(FontWeights.Normal). it worked.

                    - Regards -
                       J O N


                    A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers


                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      I agree, however IMO you're asking the wrong person. It is Chris who simply shouldn't present a "Delete" widget for messages that have replies. I have asked him many times, he eventually fixed half of it (the top level message). :)

                      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #10

                      I've asked for it on Q&A as well a few times - it really annoys me when you answer a question and it gets deleted because they (presumably) don't want the answer to help anyone else...

                      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      L 1 Reply Last reply
                      0
                      • OriginalGriffO OriginalGriff

                        I've asked for it on Q&A as well a few times - it really annoys me when you answer a question and it gets deleted because they (presumably) don't want the answer to help anyone else...

                        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        I remember some sites where it was expected to remove a problem once it got solved. CP however doesn't expect that; it does offer a "Delete" widget, and when used, you're bound to get "don't do that" comments. Pretty confusing if you ask me. :)

                        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                        OriginalGriffO 1 Reply Last reply
                        0
                        • L Luc Pattyn

                          I remember some sites where it was expected to remove a problem once it got solved. CP however doesn't expect that; it does offer a "Delete" widget, and when used, you're bound to get "don't do that" comments. Pretty confusing if you ask me. :)

                          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                          OriginalGriffO Offline
                          OriginalGriffO Offline
                          OriginalGriff
                          wrote on last edited by
                          #12

                          Yes, and I have no problem with it's existence. If you post a stupid question, and that makes you realise just how dumb you have been, then deleting it before anyone notices or answers is fine by me. But, when it is a complicated answer that can take half an hour to get right, and the OP deletes it as soon as he reads it, that does annoy me, I admit. If nothing else, it doesn't appear in any list I can access to copy it or refer anyone else to next time...

                          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups