I’m storing a collection of objects of custom class type. I’ve given the type below.
public class AnonymousClient
{
private string ipAddress, city, country, category;
private Guid id;
}
I may have to get the objects filtered based on city, country, category etc. I’m able to think of two ways –
- Storing it in a dictionary
Dictionary<Guid, AnonymousClient>
and using Linq to filter the
objects. - Storing it in a
DataTablewith
multiple columns for the members
and using DataTable.Select() to
filter the records.
I guess both of them loop internally. Which one is faster and elegant? Any insights?
Using a DataTable would add quite a bit of overhead. It’s easier to run a query once you set it up, because the code for it is already created, but it won’t run as fast.
If you want to look up items using the
idyou can use aDictionary<Guid, AnonymousClient>, if you only want to filter the data on the other fields you can just use aList<AnonymousClient>.The most efficient would be to simply loop the collection yourself and pick out the items. LINQ is almost as efficient, but it adds a slight bit of overhead. On the other hand, the LINQ code gets very simple.
Example using the LINQ extension method
Where: