C# 如何比较两个排序集?
C# How do I compare two sorted sets?
所以在我的程序中,我有一个包含 1000 个票证对象的列表。每个对象都有一个 int ID 和一个由 6 个数字组成的 int SortedSet。我希望用户能够输入六个数字,并将这 6 个数字与列表中 1000 个对象中每个对象的排序集中的 6 个六个数字进行比较。如果数字匹配,我希望输出对象的 ID。实现这一目标的最佳方式是什么?我是否也应该将用户输入的 6 个数字也放入 SortedSet 中?这就是我想做的。如果是这样,我将如何将 SortedSet 与我的列表中的 1000 个 SortedSet 中的每一个进行比较?我已经为此工作了两天,我的脑袋炸了哈哈!
希望说得通!
是的,继续将用户编号也放入 SortedSet 中,然后您可以使用以下方法查看您的工单列表中的集合是否与用户输入的集合相同。
SortedSet<int>.CreateSetComparer().Equals(userSet, objectSet);
要获取 ID 列表,您可以这样做。
IEnumerable<int> GetMatchingSetIDs(SortedSet<int> userSet)
{
IEqualityComparer<SortedSet<int>> setComparer = SortedSet<int>.CreateSetComparer();
foreach (Ticket ticket in tickets) //Where ticket is your ticket class with the sortedsets and tickets is a List of tickets.
{
if (setComparer.Equals(ticket.Set, userSet))
{
yield return ticket.ID;
}
}
}
如果我没理解错的话,那么你需要的是一个交集。
int[] array1 = { 1, 2, 3 };
int[] array2 = { 2, 3, 4 };
// Call Intersect extension method.
var intersect = array1.Intersect(array2);
// Write intersection to screen.
foreach (int value in intersect)
{
Console.WriteLine(value);
}
你可以使用SortedSet.SetEquals()方法。
https://docs.microsoft.com/en-US/dotnet/api/system.collections.generic.sortedset-1.setequals
所以在我的程序中,我有一个包含 1000 个票证对象的列表。每个对象都有一个 int ID 和一个由 6 个数字组成的 int SortedSet。我希望用户能够输入六个数字,并将这 6 个数字与列表中 1000 个对象中每个对象的排序集中的 6 个六个数字进行比较。如果数字匹配,我希望输出对象的 ID。实现这一目标的最佳方式是什么?我是否也应该将用户输入的 6 个数字也放入 SortedSet 中?这就是我想做的。如果是这样,我将如何将 SortedSet 与我的列表中的 1000 个 SortedSet 中的每一个进行比较?我已经为此工作了两天,我的脑袋炸了哈哈!
希望说得通!
是的,继续将用户编号也放入 SortedSet 中,然后您可以使用以下方法查看您的工单列表中的集合是否与用户输入的集合相同。
SortedSet<int>.CreateSetComparer().Equals(userSet, objectSet);
要获取 ID 列表,您可以这样做。
IEnumerable<int> GetMatchingSetIDs(SortedSet<int> userSet)
{
IEqualityComparer<SortedSet<int>> setComparer = SortedSet<int>.CreateSetComparer();
foreach (Ticket ticket in tickets) //Where ticket is your ticket class with the sortedsets and tickets is a List of tickets.
{
if (setComparer.Equals(ticket.Set, userSet))
{
yield return ticket.ID;
}
}
}
如果我没理解错的话,那么你需要的是一个交集。
int[] array1 = { 1, 2, 3 };
int[] array2 = { 2, 3, 4 };
// Call Intersect extension method.
var intersect = array1.Intersect(array2);
// Write intersection to screen.
foreach (int value in intersect)
{
Console.WriteLine(value);
}
你可以使用SortedSet.SetEquals()方法。
https://docs.microsoft.com/en-US/dotnet/api/system.collections.generic.sortedset-1.setequals