簡體   English   中英

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM