Linq - returns normal elements from n number of lists

I am writing a simple search page for our intranet application. When a user searches for n words, I create n hit lists and then want to return only those results that are common to all lists.

I have something working with help List<int>like this:

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 is 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 do I modify a query to return the result I want?

+3
source share
2 answers

, SearchResult , . , IEquatable<T>. http://msdn.microsoft.com/en-us/library/ms131187(v=vs.110).aspx

..

public class SearchResult : IEquatable<SearchResult>
{
    public string Url{get;set;}
    public string SearchText { get; set; }
    public bool Equals(SearchResult other)
    {
        if (other == null)
            return false;
        return string.Concat(this.Url, this.SearchText).Equals(string.Concat(other.Url, other.SearchText), StringComparison.OrdinalIgnoreCase);
    }
}

IEqualityComparer<T> Enumerable.Intersect.

+3

Enumberable.Intersect . . IEqualityComparer .

SearchResult

public class SearchResult
    {
        public SearchResult(string first, string second)
        {
            First = first;
            Second = second;
        }

        public string First { get; set; }
        public string Second { get; set; }

    }

IEqualityComparer.

 public class MyCompare : IEqualityComparer<SearchResult>
    {
        public bool Equals(SearchResult x, SearchResult y)
        {
            return (x.First == y.First) && ((x.Second == y.Second));
        }


        public int GetHashCode(SearchResult obj)
        {
            unchecked
            {
                int hash = 17;
                hash = hash * 23 + obj.First.GetHashCode();
                hash = hash * 23 + obj.Second.GetHashCode();
                return hash;
            }
        }
}

GetHashCode, . , ,

return obj.A.GetHashCode();

. , . @Tim

Intersect ,

var common = list1.Intersect<SearchResult>(list2,new MyCompare()).ToList();
+1

All Articles