I have class like below
public class Item { public long Id {get;set;} public long GroupingId {get;set;} public long Weight {get;set;} public long Tolerance {get;set;} } Now I have list of Items with different grouping id. Lets say
List<Item> items = GetItems(); Now I need to group based groupings id, and make check for each item in that group against each other. How would I do that in LINQ efficiently. Any help much appreciated.
IDictionary<long, long[]> matches = new Dictionary<long, long[]>(); foreach(groupedItems in items.GroupBy(p=>p.GroupingId)) { foreach(item in groupItems) { // Check item with other items in group items // and if condition is correct take those 2 items. // lets say the condition is // (item.Weighting - other.Weighting) > item.Tolerance // duplicates could be removed // lets condition for 1,2 is done means no need to do 2 against 1 var currentItem = item; var matchedOnes = groupItems.Where(p => (Math.Abs(p.Weighting - currentItem.Weighting) > currentItem .Tolerance) && p.Id != currentItem.Id) .ToList(); if (!matchedOnes.Any()) continue; matches.Add(currentItem.Id, matchedOnes .Select(p=>p.Id).ToArray()); } } I did like above, but its giving duplicates(1,2 and 2,1 are duplicates).. How would I remove the duplicate checks