I have a List<> which contains another List<>
I need to find if a given value is present in any of the items in the innermost list.
If match found, I need that specific item and return.
I am doing this as shown below:
InnerList inner = null;
foreach(TopList in topListItems)
{
inner = asn.Owners.Find(x => x.GuestId == guestId);
if(inner != null)
break;
}
//item found if inner is not null
//else item absent in the inner list
Any other alternate way that may run faster than this?
EDIT:
Some correction: I just need to see if inner list has an item with a specific value.
If yes, then I need to return the top level item that that has the match.
I guess the logic is the same.
If you want to keep the data structure then the only improvement I see is throwing out the delegate based search and search manually. I expect an improvement of about factor two with that.
If possible you could employ dictionaries in some way. But I don’t know enough about your problem to tell you if that’s possible. This can give a really big speedup since a search by key in a dictionary is O(1) and not just O(n).
In some situations a
forloop might give a slight speedup over theforeachloop, but I don’t know if this is one of them. So you’ll need to benchmark.