简体   繁体   中英

Best way to compare two large string lists, using C# and LINQ?

I have a large list (~ 110,000 strings), which I need to compare to a similar sized list.

List A comes from 1 system. List B comes from a SQL table (I can only read, no stored procs, etc)

What is the best way to find what values are in list A, that no longer exists in list B?

Is 100,000 strings a large number to be handled in an array?

thanks

So you have two lists like so:

List<string> listA;
List<string> listB;

Then use Enumerable.Except :

List<string> except = listA.Except(listB).ToList();

Note that if you want to, say, ignore case:

List<string> except = listA.Except(listB, StringComparer.OrdinalIgnoreCase).ToList();

You can replace the last parameter with an IEqualityComparer<string> of your choosing.

使用LINQ:

var missing = listA.Except(listB).ToList();

Out of interest, do you HAVE to use List<string> ? Because in .net 3.5 SP1, you can use the HashSet and it's ExceptWith method. To my understanding, HashSets are specifically optimized for comparisons between two Sets.

List<string> A = //get from file
List<string> B = //get from db

var C = A.Except(B);

这个问题中窃取,看起来你可以使用Except<T>()方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM