[英]Intersection of two sets (Lists) of data
我有兩組數據(復雜對象列表或SQL數據 - LINQ to Entities),我試圖找到兩組數據的交集。 具體是Complex屬性的交集,“HashData”如下所示:
左側的集合可能約為10000行,而右側的集合總是約100行的子集。 我意識到,如果我在存儲它時將“Hashdata”左側的集合排序,使用某種二進制搜索算法進行搜索會快得多,但是由於與問題無關的原因我無法做到這一點。
較小的數據子集永遠不會存儲在SQL中(出於解釋目的,僅在下面的SQL表中顯示)。 它在運行時顯示在List<ShowData>
中。
目前我正在做一個可憐的循環數據和匹配這樣(其中recording
是100行List和ShowData
是10000行列表):
List<ShowData> ShowData = (from showData in context.ShowDatas
where (showData.Show.Id == advert.Id)
orderby showData.HashData ascending
select showData).ToList();
foreach (ShowData recording in recordingPoints) {
foreach (ShowData actual in ShowData) {
if (recording.HashData == actual.HashData) {
}
}
}
基本上我想要做的是:
返回ShowData對象(大集)列表,其中在LINQ to Entity初始查詢中的ShowData BUT中找到任何HashData(來自小集)。
我接近:
private IEnumerable<ShowData> xyz(List<ShowData> aObj, List<ShowData> bObj)
{
IEnumerable<string> bStrs = bObj.Select(b => b.HashData).Distinct();
return aObj.Join(bStrs, a => a.HashData, b => b, (a, b) => a);
}
由於您使用的是IEnumerable,因此可以使用Intersect Extension方法而不是Join。 如果要返回大集合,則需要將大集合查詢的結果與較小集合相交。 你需要編寫一個IEquality Comparer,如下所示: http : //msdn.microsoft.com/en-us/library/bb355408.aspx來比較你的對象,然后調用Intersect擴展方法:
return bStrs.Intersect(aObj, new MyEqualityComparer());
這樣的事情可能會起作用(警告未經測試):
private IEnumerable<ShowData> xyz(List<ShowData> aObj, List<ShowData> bObj)
{
return aObj.Where(sd1 => bObj.Select(sd2 => sd2.HashData).Contains(sd1.HashData));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.