I'm writing a simple search page for our intranet application. When a user searches for n words I create n lists of hits and then want to return only the results that are common to all lists.
I have something working using List<int>
thus:
var list1 = new List<int> {1,2,3,4,5,8,10};
var list2 = new List<int> {3,5,6,9,10,11};
var list3 = new List<int> {3,4,5,10};
var listOfLists = new List<List<int>>{list1,list2,list3};
var results = listOfLists.Aggregate((prevList, nextList) => prevList
.Intersect(nextList)
.ToList());
results.Dump(); //correctly returns 3,5,10, which are common to all lists
However if I try this with my SearchResults class I get no results. Here's the code:
//results from first search word "howard"
List<SearchResult> list1 = new List<SearchResult>();
list1.Add(new SearchResult ("a.html","howard kent"));
list1.Add(new SearchResult ("b.html","howard shaw")); //the common item
list1.Add(new SearchResult ("c.html","howard smith"));
list1.Add(new SearchResult ("d.html","howard moore"));
//results from second search word "shaw"
List<SearchResult> list2 = new List<SearchResult>();
list2.Add(new SearchResult ("e.html","jon shaw"));
list2.Add(new SearchResult ("b.html","howard shaw")); //the common item
list2.Add(new SearchResult ("f.html","chris shaw"));
//could be n further lists...
//make a list of lists
List<List<SearchResult>> searchLists = new List<List<SearchResult>>();
searchLists.Add(list1);
searchLists.Add(list2);
//find results that are common to all lists.
//Doesn't work - returns nil items, should return 1
var results = searchLists
.Aggregate((prevList, nextList) => prevList
.Intersect(nextList)
.ToList());
results.Dump();
}
class SearchResult
{
public string Url{get;set;}
public string SearchText { get; set; }
//constructor
public SearchResult(string url,string searchText)
{
Url = url;
SearchText = searchText;
}
How should I change the query to return the result I want?