I would like to change the way the source list is:
_context.SimilarityMatrix.Where(Row => itemIds.Contains(Row.FirstIndex) || itemIds.Contains(Row.SecondIndex)) .Select(r => new { r.MatrixId, r.FirstIndex, r.SecondIndex, r.Similarity, MatchingIndex = itemIds.Contains(r.FirstIndex) ? r.FirstIndex : r.SecondIndex }) .Distinct() .ToList();
Thus, you only need to group by the compliance index.
var itemsNotReviewed = Similarities. .GroupBy(x => x.MatchingIndex) .ToList();
You can convert after a dynamic object to your affinity class or just change the class to include the corresponding index.
You can convert them to your type of affinity:
var itemsNotReviewed = Similarities. .GroupBy(x => x.MatchingIndex) .Select(g => new { g.Key, Values = g.Values.Select(d => new Similarity { MatrixId = d.MatrixId, FirstIndex = d.FirstIndex, SecondIndex = d.SecondIndex, Similarity = d.Similarity }).ToList() }) .ToList();
source share