Quickly getting a subset of properties used in a huge collection in C #

I have a huge collection (which I can use as an enumerated use of OfType <> () objects). Each of these objects has a property Categorythat is retrieved from a list elsewhere in the application. This collection can reach the size of hundreds of items, but it is possible that only, say, 6/30 of the possible categories are actually used. What is the fastest way to find these 6 categories? The size of the huge collection keeps me from simply sorting through the whole thing and returning all unique values, so is there a faster way to accomplish this?

Ideally, I collected categories in List<string>.

+3
source share
2 answers

If you are using .NET 3.5, try the following:

List<string> categories = collection
    .Cast<Foo>()
    .Select(foo => foo.Category)
    .Distinct()
    .ToList();

It should be very fast.

I assume that these objects were originally obtained from the database? If so, you can ask the database to do the work for you. If there is an index in this column, you will immediately get the result immediately, without even bringing the objects into memory.

+2
source

The size of the huge collection prevents me from simply sorting through the whole thing and returning all unique values

I’m afraid that to find all the categories used, you will need to look at each item once, so you are unlikely to avoid repetition (if you do not track the categories used when creating your collection).

, Mark Byers , .

0

Source: https://habr.com/ru/post/1743366/


All Articles