2017-12-13 11 views
0

아래의 쿼리는 내가 원하지만, 두 목록에 많은 항목 (> 300000)이있을 때 매우 느립니다.두 목록을 비교하는 LINQ 쿼리의 성능을 향상시키는 방법은 무엇입니까?

기본적으로, 목록에있는 문서 1.

 personList1.Add(person1); 
     personList1.Add(person2); 

     personList2.Add(person2); 
     personList2.Add(person3); 

     var result = personList2 
        .Where(p2 => p2.documents 
         .Exists(d2 => !personList1 
          .Exists(p1 => p1.documents 
           .Contains(d2) 
          ) 
         ) 
        ).ToList(); 

     result.ForEach(r => Console.WriteLine(r.name)); 
     //Should return person3 name 

클래스

public class Person 
{ 
    public string name { get; set; } 
    public List<IdentificationDocument> documents { get; set; } 

    public Person() 
    { 
     documents = new List<IdentificationDocument>(); 
    } 
} 

public class IdentificationDocument 
{ 
    public string number { get; set; } 
} 

https://dotnetfiddle.net/gS57gV

누구나 알고 전체 코드가없는 사람 목록 2에있는 모든 사람들을 반환 쿼리 성능을 향상시키는 방법? 고맙습니다!

var lookup = new HashSet<string>(personList1.SelectMany(p => p.documents).Select(d => d.number)); 

var result = personList1.Where(p => !p.documents.Select(d => d.number).Any(lookup.Contains)); 
+0

왜'John' : – Sinatr

+0

'Person' 클래스를 변경할 수 있다면'List '를'HashSet '로 변경하십시오. 'GetHashCode'를 오버라이드하고 싶을 수도 있습니다. –

답변

2

먼저 조회를 위해 만든 구조에 관련된 모든 데이터를 넣어?