I have a property called SelectedSections that is assigned from a collection of Sections. Each Section holds a collection of BidItems that contains 1,000+ items in it. When I select a Section, I need to refresh my collection of items that the view is databound to, with a filtered set of items.
public Section SelectedSection
{
get
{
return selectedSection;
}
set
{
this.SetPropertyByReference(ref this.selectedSection, value);
if (value != null)
{
this.BidItems = value.BidItems
.Where(item =>
!item.Description.ToLower().Contains("flagger") ||
!item.Description.ToLower().Contains("civilian flagger") ||
!item.Description.ToLower().Contains("law enforcement"))
.ToList();
}
this.MptPayment.EditedItem.DiaryPayItem.Section = value;
}
}
I have to filter out about a dozen or so different types of items (I only show 3 for clarity sake). In my Where
clause, I am converting everything to lower case before I check if the collection holds what I'm filtering out or not.
I realize this is going to generate a lot of garbage, as every one of the 1,000+ items in the collection will have a new string created for the lower-case Description
content. My question is, would doing this a dozen times for every item in the collection be more expensive than my just checking all of the known variations? Disregarding the fact that I might miss a variation as I'm more interested in the theory of which is faster.
- Flagger
- flagger
- FLAGGER
The above list are all of the known variations. I'm wondering which would be the more expensive route to go. Iterating over the collection to check each of the known conditions would be fast enough, without the overhead of generating so much garbage. It's either, enumerate more than once per item/description in order to find them all, or enumerate once per item/description while creating garbage strings on the heap along the way that will be GC'd.
Note that this property could be re-set several dozen times as the user performs their work. So a lot (tens of thousands) of string comparisons will be performed.
I realize this is a cheap operation relative to the rest of the app; I'm wanting to know more for educating myself rather than worrying about a performance hit in my actual application.