簡體   English   中英

比較3個列表與不同類型數據的最佳方法

[英]Best way to compare 3 lists with different types of data

我試圖找到最好的方法來比較C#中的3個列表,然后將結果合並到一個csv文件中。 我的代碼需要花費大量時間來處理我的數據,我想對此進行改進。

目前,我要做的是使用LINQ一次比較兩個列表,將每個比較的結果保存到一個臨時列表中,然后合並它,然后再將其保存為csv文件。

我的列表之一的示例進行了比較:

foreach (RemedyAsset remedyAsset in RemedyAssetsList)
        {
            MIAsset tempMIAsset = null;

            tempMIAsset = MIAssetsList.FirstOrDefault(m => m.CIName.ToUpper() == remedyAsset.CIName.ToUpper());

            if (tempMIAsset == null)
            {
                TempAssets.Add(new TempAsset
                {
                    Asset = remedyAsset.CIName,
                    LastMiActivity = string.Empty,
                    RemedyStatus = remedyAsset.Status,
                    InMI = false,
                    InRemedy = true
                });
            }
            else
            {
                TempAssets.Add(new TempAsset
                {
                    Asset = remedyAsset.CIName,
                    LastMiActivity = tempMIAsset.LastActiveTime,
                    RemedyStatus = remedyAsset.Status,
                    InMI = true,
                    InRemedy = true
                });
            }
        }

我的列表來自3個不同的IT系統(BMC Remedy,Active Directory,Checkpoint),它們具有一個公共變量:資產編號。

我的列表如下所示:

-(List1)RemedyReport:資產編號-補救狀態。
-(List2)ADReport:資產編號-上次登錄時間-上次密碼更改。
-(List3)MIReport:資產編號-上次服務器聯系。

比較列表時,我還會檢查列表中是否不存在資產編號,這也需要在輸出的csv文件中顯示。 然后,我需要合並存在匹配資產編號的列表中的數據,並且輸出將如下所示:
資產編號-解決方法-MI中-AD中-上次服務器聯系-上次登錄-上次密碼更改

比較3個列表的最佳方法是什么?
比較3個或更多列表是否有最佳實踐?

這是我在這里的第一篇文章,如果做錯任何事情,請告訴我。

基於Samar的建議的解決方案
我對Samar的建議進行了調整,從而滿足了我的需求。 性能從9分鍾提高到不到2分鍾。

public void compare()
        {
            //Creating dummy data for testing
            List<C1> L1 = new List<C1>() { new C1() { ID = 1, classC1="test1" }, new C1() { ID = 4, classC1="test1" } };
            List<C2> L2 = new List<C2>() { new C2() { ID = 1, classC2="test2" }, new C2() { ID = 2, classC2="test2" } };
            List<C3> L3 = new List<C3>() { new C3() { ID = 1 }, new C3() { ID = 2, classC3="test3" }, new C3() { ID = 3, classC3="test3" } };

            //Creating new list which will contain all the objects without duplicates based on ID column
            List<C4> L4 = new List<C4>();

            //Firstly add all the objects from L1
            L4.AddRange(from l1 in L1 select new C4() { ID = l1.ID, classC1=l1.classC1 });

            //Add only those objects from L3 which are not part of L1
            L4.AddRange(from l22 in L2
                        where !(L4.Where(l44 => l44.ID == l22.ID)
                                .Select(l44 => { l44.classC2 = l22.classC2; return l44; }).Any(p => p.ID == l22.ID))
                        select new C4() { ID = l22.ID, classC2 = l22.classC2 });

            //Add only those objects from L3 which are not part of L1 and L2
            L4.AddRange(from l33 in L3
                        where !(L4.Where(l44 => l44.ID == l33.ID)
                                .Select(l44 => { l44.classC3 = l33.classC3; return l44; }).Any(p => p.ID == l33.ID))
                        select new C4() { ID = l33.ID, classC3 = l33.classC3 });

            //L4 will now contain all IDs without duplicates
        }

        class C1
        {
            public int ID { get; set; }
            public string classC1 { get; set; }

            //will contain other properties
        }

        class C2
        {
            public int ID { get; set; }
            public string classC2 { get; set; }

            //will contain other properties
        }

        class C3
        {
            public int ID { get; set; }
            public string classC3 { get; set; }

            //will contain other properties
        }

        class C4
        {
            public int ID { get; set; }
            public string classC1 { get; set; }
            public string classC2 { get; set; }
            public string classC3 { get; set; }

            //will contain other properties or maybe a combination of all the properties of C1, C2 and C3
        }

我無法理解您的課程結構,因此我將嘗試以更通用的方式回答您的問題。

首先,讓我了解您的確切需求。

假設您有3個清單,其中有3個不同的類別。 因此,您具有列表L1,L2和L3,其中分別是類C1,C2和C3。

您需要一個列表,說L4以及另一個(或上面的一個?)類,說C4,並且此列表不應包含基於所有3類中某些共同屬性的重復項。

我對這個問題的理解是下面的代碼。 我不太確定這是否有效,但確實有效。 它也不是通用的,即適用於具有實現某個接口的任何類集的任何數量的列表。 嘗試一下,讓我知道在性能方面是否正在造成問題或以其他任何方式解決此問題。

我希望這有幫助。

問候,

薩馬爾

    private void btnCompare_Click(object sender, EventArgs e)
    {
        //Creating dummy data for testing
        List<C1> L1 = new List<C1>() { new C1() { ID = 1 }, new C1() { ID = 4 } };
        List<C2> L2 = new List<C2>() { new C2() { ID = 1 }, new C2() { ID = 2 } };
        List<C3> L3 = new List<C3>() { new C3() { ID = 1 }, new C3() { ID = 2 }, new C3() { ID = 3 } };

        //Creating new list which will contain all the objects without duplicates based on ID column
        List<C4> L4 = new List<C4>();

        //Firstly add all the objects from L1
        L4.AddRange(from l1 in L1 select new C4() { ID = l1.ID });

        //Add only those objects from L2 which are not part of L1
        L4.AddRange(from l22 in L2
                    where !(from l4 in L4 join l2 in L2 on l4.ID equals l2.ID select new { ID = l4.ID }).Any(p => p.ID == l22.ID) 
                    select new C4() { ID = l22.ID });

        //Add only those objects from L3 which are not part of L1 and L2
        L4.AddRange(from l33 in L3
                    where !(from l4 in L4 join l3 in L3 on l4.ID equals l3.ID select new { ID = l4.ID }).Any(p => p.ID == l33.ID)
                    select new C4() { ID = l33.ID });

        //L4 will now contain all IDs without duplicates
    }

    class C1
    {
        public int ID { get; set; }

        //will contain other properties
    }

    class C2
    {
        public int ID { get; set; }

        //will contain other properties
    }

    class C3
    {
        public int ID { get; set; }

        //will contain other properties
    }

    class C4
    {
        public int ID { get; set; }

        //will contain other properties or maybe a combination of all the properties of C1, C2 and C3
    }

暫無
暫無

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

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