Amin Mohammadi
Amin Mohammadi

Reputation: 1

Linq contains for compairing two huge list takes long time

When i want to compare a huge list (about 700,000 elements) with a specific property and list of string, takes long time.

I tried AsParallel but it doesn't help me any more. i need list for removedSuccessFromList because i want to use this list for start a Parallel.Foreach

List<string> successStrings = service.GetProperty().Select(q => 
q.IdString).ToList();

List<Property> removedSuccessFromList = properties.AsParallel().Where(q => 
!successStrings.Contains(q.IdString)).ToList();

Upvotes: 0

Views: 546

Answers (2)

Backs
Backs

Reputation: 24903

Use mre effective data structure if you have lot of strings in successStrings, like hash set:

var successStrings = new HashSet<string>(service.GetProperty().Select(q => q.IdString));

List<Property> removedSuccessFromList = properties.Where(q => !successStrings.Contains(q.IdString)).ToList();

List.Contains method has complexity O(N), so it scan all elements to find match. HashSet.Contains has complexity O(1) - it can check if element exists very vast.

Upvotes: 3

Thaina Yu
Thaina Yu

Reputation: 1512

If your IdString is unique maybe you could remove each founded item from successStrings in the Where logic so the list get smaller eventually

Upvotes: 0

Related Questions