A better way
-
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
}
} -
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
}
}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
-
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
}
} -
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
}
}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
-
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
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 )) ???
-
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 )) ???
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
-
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
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
ENDJust incase somebody else needed to know :-D