protected Dictionary <int, int> ChooseBestPairs() { CanonDataContext db = Cdb.Instance; Dictionary <int, int> result = new Dictionary <int, int>(500); var bestMap = db.GetBestRelevanceForMapping(this._channelId).OrderByDescending(p => p.RelevancePercent); List <GetBestRelevanceForMappingResult> list = bestMap.ToList(); var grouped = db.GetBestRelevanceForMapping(this._channelId).GroupBy(g => g.ProductId); List <int> groupedList = new List <int>(100); List <MappingRule> currentRules = db.MappingRules.Where(c => c.ChannelId == this._channelId).ToList(); foreach (var pc in grouped) { if (pc.Count() == 1) { groupedList.Add(pc.Key); } } //first add items which have one2one pair foreach (GetBestRelevanceForMappingResult mr in list) { if (!groupedList.Contains(mr.ProductId)) { continue; } if (result.ContainsValue(mr.ChannelMonitorId)) { continue; } if (this.IsForbiddenToRecommend(currentRules, mr.ProductId, mr.ChannelMonitorId)) { continue; } result.Add(mr.ProductId, mr.ChannelMonitorId); } //now add the rest foreach (GetBestRelevanceForMappingResult mr in list) { if (result.ContainsKey(mr.ProductId)) { continue; } if (result.ContainsValue(mr.ChannelMonitorId)) { continue; } if (this.IsForbiddenToRecommend(currentRules, mr.ProductId, mr.ChannelMonitorId)) { continue; } result.Add(mr.ProductId, mr.ChannelMonitorId); } return(result); }