it has a property:
string Code
and 10 other.
common codes is list of strings(string[] )
cars a list of cars(Car[])
filteredListOfCars is List.
for (int index = 0; index < cars.Length; index++)
{
Car car = cars[index];
if (commonCodes.Contains(car.Code))
{
filteredListOfCars.Add(car);
}
}
Unfortunately this piece of methodexecutes too long.
I have about 50k records
How can I lower execution time??
Jared has correctly pointed out that you can optimize this with a
HashSet, but I would also like to point out that the entire method is unnecessary, wasting memory for the output list and making the code less clear.You could write the entire method as:
Execution of the
filteredCarsfiltering operation will be deferred, so that if the consumer of it only wants the first 10 elements, i.e. by usingfilteredCars.Take(10), then this doesn’t need to build the entire list (or any list at all).