linq-从n个列表中返回常见项目 [英] linq - return common items from n number of lists
问题描述
我正在为我们的Intranet应用程序编写一个简单的搜索页面.当用户搜索n个单词时,我会创建n个匹配列表,然后只返回所有列表共有的结果.
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.
我正在使用List<int>
进行某些操作:
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
但是,如果我在SearchResults类中尝试使用此方法,则不会获得任何结果.这是代码:
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?
推荐答案
这是因为尽管SearchResult
对象具有相同的数据,但它们不是相同的对象.您可以通过实现IEquatable<T>
接口来解决此问题. http://msdn.microsoft.com/zh- us/library/ms131187(v = vs.110).aspx
That's because the SearchResult
objects are not the same objects, although they have the same data. You can fix this by implementing the IEquatable<T>
interface. 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方法.
Another solution would be to implement a IEqualityComparer<T>
and pass it on to the Enumerable.Intersect method.
这篇关于linq-从n个列表中返回常见项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!