我有像下面这样的课
public class Item
{
public long Id {get;set;}
public long GroupingId {get;set;}
public long Weight {get;set;}
public long Tolerance {get;set;}
}
现在我有Items
不同分组 ID 的列表。让我们说
List<Item> items = GetItems();
现在我需要对基于分组的 id 进行分组,并检查该组中的每个项目。我将如何在LINQ中有效地做到这一点。非常感谢任何帮助。
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());
}
}
我确实喜欢上面,但它给出了重复项(1,2 和 2,1 是重复项).. 我将如何删除重复检查