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. A better way

A better way

Scheduled Pinned Locked Moved C#
question
7 Posts 5 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.
  • S Offline
    S Offline
    Sevententh
    wrote on last edited by
    #1

    Is there a better way to code the second foreach...?

    bool blnAccepted = false;
    IList<Country> ftList = svc.GetCountryList;
    IList<County> CountyList = svc.GetCountyList;

                foreach (Country c in ftList)
                {
                    blnAccepted = false;
                    **foreach (County ic in CountyList)
                    {
                        if (ic.County == c.County)
                        {
                            blnAccepted = true;
                            break;
                        }
                    }**
    
                    if (blnAccepted)
                    {
    

    //Do work
    }
    }

    C D K 3 Replies Last reply
    0
    • S Sevententh

      Is there a better way to code the second foreach...?

      bool blnAccepted = false;
      IList<Country> ftList = svc.GetCountryList;
      IList<County> CountyList = svc.GetCountyList;

                  foreach (Country c in ftList)
                  {
                      blnAccepted = false;
                      **foreach (County ic in CountyList)
                      {
                          if (ic.County == c.County)
                          {
                              blnAccepted = true;
                              break;
                          }
                      }**
      
                      if (blnAccepted)
                      {
      

      //Do work
      }
      }

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Yes, I would imagine that creating a map of countries to counties would allow you to do a single lookup. I'm not sure I follow your overall logic, but if you're doing this second loop a lot, a lookup in a hashtable will make it quicker. That's assuming you need to lookup a county for a country. If you just want to see if it exists, then keep a sorted list, so you can perform a search on it, faster than looking through the whole list.

      Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

      1 Reply Last reply
      0
      • S Sevententh

        Is there a better way to code the second foreach...?

        bool blnAccepted = false;
        IList<Country> ftList = svc.GetCountryList;
        IList<County> CountyList = svc.GetCountyList;

                    foreach (Country c in ftList)
                    {
                        blnAccepted = false;
                        **foreach (County ic in CountyList)
                        {
                            if (ic.County == c.County)
                            {
                                blnAccepted = true;
                                break;
                            }
                        }**
        
                        if (blnAccepted)
                        {
        

        //Do work
        }
        }

        D Offline
        D Offline
        dan sh
        wrote on last edited by
        #3

        Use Contains method in of the list.

        1 Reply Last reply
        0
        • S Sevententh

          Is there a better way to code the second foreach...?

          bool blnAccepted = false;
          IList<Country> ftList = svc.GetCountryList;
          IList<County> CountyList = svc.GetCountyList;

                      foreach (Country c in ftList)
                      {
                          blnAccepted = false;
                          **foreach (County ic in CountyList)
                          {
                              if (ic.County == c.County)
                              {
                                  blnAccepted = true;
                                  break;
                              }
                          }**
          
                          if (blnAccepted)
                          {
          

          //Do work
          }
          }

          K Offline
          K Offline
          kb boxer
          wrote on last edited by
          #4

          If you are using C# 3.X and above, you can use the extension methods of IEnumerable; the one you are looking for is Intersect. So ftList.Intersect(CountyList) gives you a collection of all elements of ftList that are present in CountyList. Hope that helps!

          http://vivekragunathan.spaces.live.com

          Programming is an art. Code is a poem

          S 1 Reply Last reply
          0
          • K kb boxer

            If you are using C# 3.X and above, you can use the extension methods of IEnumerable; the one you are looking for is Intersect. So ftList.Intersect(CountyList) gives you a collection of all elements of ftList that are present in CountyList. Hope that helps!

            http://vivekragunathan.spaces.live.com

            Programming is an art. Code is a poem

            S Offline
            S Offline
            Sevententh
            wrote on last edited by
            #5

            How does this work for different classes? I simplified the code earlier, so this is what I have: Table Def: Locations: ID, Description, TypeID, ParentID LocationType: TypeID, Description Rows in Location: 1 England 1 0 2 France 1 0 3 London 2 1 4 Hampshire 2 1 5 Dorest 2 1 6 Portsmouth 3 4 6 Paris 2 2 etc.... Rows in LocationType: 1 Country 2 Regions 3 Cities 4 Hospitals 5 Hotels I now have a hierarchy of locations, each with a specific type. So how do I use ftList.Intersect(Type) on just the TypeID? Foreach (ftList.Intersect(TypeID = 2 | 4 | 5 )) ???

            S 1 Reply Last reply
            0
            • S Sevententh

              How does this work for different classes? I simplified the code earlier, so this is what I have: Table Def: Locations: ID, Description, TypeID, ParentID LocationType: TypeID, Description Rows in Location: 1 England 1 0 2 France 1 0 3 London 2 1 4 Hampshire 2 1 5 Dorest 2 1 6 Portsmouth 3 4 6 Paris 2 2 etc.... Rows in LocationType: 1 Country 2 Regions 3 Cities 4 Hospitals 5 Hotels I now have a hierarchy of locations, each with a specific type. So how do I use ftList.Intersect(Type) on just the TypeID? Foreach (ftList.Intersect(TypeID = 2 | 4 | 5 )) ???

              S Offline
              S Offline
              S Senthil Kumar
              wrote on last edited by
              #6

              For arbitrary filtering, use the Where extension method - you pass in a predicate to that method which can use any of the properties of your object.

              Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

              S 1 Reply Last reply
              0
              • S S Senthil Kumar

                For arbitrary filtering, use the Where extension method - you pass in a predicate to that method which can use any of the properties of your object.

                Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                S Offline
                S Offline
                Sevententh
                wrote on last edited by
                #7

                I found that rewriting the SQL as a stored procedure was easier to get the hierarchy that I needed

                CREATE PROC sp_Hierarchy
                (@ParentID int)
                AS
                BEGIN
                WITH _items(ID, ParentID, TypeID, Depth)
                AS (
                SELECT ID, ParentID, TypeID, 0 AS Depth FROM tblItems WHERE ID = @ParentID
                UNION ALL
                SELECT t.ID, t.ParentID, t.TypeID, i.Depth + 1 FROM tblItems t JOIN _item i ON t.ParentID = i.ID
                )
                SELECT * FROM _item
                END

                Just incase somebody else needed to know :-D

                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