English 中文(简体)
What is the most efficient way to create a distinct list of items using .NET?
原标题:

I have a large list of values (100-200 character strings) and I need to return a distinct listing of them. What is the most efficient way to do this using .NET? The 2 ways that I can think of are:

  1. Use the Distinct() method of the IEnumerable class
  2. Use a Dictionary

If the Dictionary approach is faster in raw terms, consider a trade-off decision around maintainability of code.

最佳回答

I would expect Enumerable.Distinct to be about as fast as using a dictionary if you re only doing it once. If you want to be able to add/remove values and keep the distinct-ness, you could build a HashSet<string> (which is basically what I expect Distinct is doing under the hood, but Distinct() will obviously return new values as it finds them, maintaining order.

In fact, just using:

HashSet<string> distinctItems = new HashSet<string>(list);

will be a pretty good (and simple) solution if you don t mind the ordering being messed up. It s simpler than using a Dictionary, and conceptually cleaner as well (as you don t really want to map keys to values).

(As ever, I would suggest finding the most readable solution first, and benchmark it - if it s "fast enough" then go with that. If you want to use this as part of another query, then Distinct may well be the most readable way. Otherwise, I d suggest HashSet.)

问题回答

I would personally go with the Distinct() method provided by LINQ. It s far easier to read and maintain. Whilst using LINQ will be slower than using a dictionary the difference will be small (in the case you ve listed) and you d be better spending time optimizing database queries or web service calls.

I would siggest you to use profiling here. Generate a list with sample items, sort it say 1M times using both ways, and measure the time used by each way.

If readability is a concern, create a GetDistinctItems method and put your code inside it: voilà, self-documented code.





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签