簡體   English   中英

在 C# 中合並兩個對象的有效方法

[英]Efficient way to Merge two objects in C#

我在 c# 中有兩個對象。 一個用於 Header 詳細信息(字典),另一個用於交易。 兩者都來自不同的系統。

第一個 Object -(類型為 NameValueCollection 或存在此類映射的任何 object)

    Name: "NameOfA"
    Value : "A1"  //Name of the property in second object

    Name: : "NameOfB"
    Value: "B1"  //Name of the property in second object

    Name: : "NameOfC"
    Value: "C1"  //Name of the property in second object

    Name: : "NameOfD"
    Value: "D1"  //Name of the property in second object

第二個 Object (自定義類型/JObject) -

    "Property 1" : "Property 1 Value",
    "Property 2" : "Property 2 Value",
    "Property 3" : "Property 3 Value",
    "Property 4:  {
        [
            {
                "A1" : "Value of A1", //The key "A1" here maps to the value property of First object
                "Other Property 1" : "Value of other property 1",
                "Other Property 2" : "Value of other property 2",
                "Other Property 3" : 100
            },
            
            {
                "B1" : "Value of B1",
                "Other Property 1" : "Value of other property 1",
                "Other Property 2" : "Value of other property 2",
                "Other Property 3" : 100
            },

            {
                "C1" : "Value of C1",
                "Other Property 1" : "Value of other property 1",
                "Other Property 2" : "Value of other property 2",
                "Other Property 3" : 100
            },
            {
                "D1" : 100; // Value of D1
                "Other Property 1" : "Value of other property 1",
                "Other Property 2" : "Value of other property 2",
                "Other Property 3" : 100
            },
                
        ]
    
    }

兩個 object 之間的關系是,First object 的值是 Second object 中的Key (在子數組中)。

例如 -值:“A1” ,第一個 object 的值“A1”是第二個 object 的關鍵:

"Property 4:  {
        [
            {
                *"A1"* : "Value of A1": 

我想一起處理這兩個對象,所以我可以得到以下格式:

[
        {
            "NameOfTask": "NameOfA",   // Name property of first object
            "ValueOfTask": "Value of A1" //Value of Property A1 in second object
        },

        {
            "NameOfTask": "NameOfB",
            "ValueOfTask": "Value of B1"
        },

        {
            "NameOfTask": "NameOfC",
            "ValueOfTask": "Value of C1"
        },

        {
            "NameOfTask": "NameOfD",
            "ValueOfTask": 100
        }


    ]

處理這兩個不同的 object 的最快算法可能是什么,因為 object 結構是完全自定義的,在我的第一個 object 中可能有 100 條記錄,第二條記錄可能有 1000 條。

提前致謝。

您可以使用Dictionary<>臨時存儲兩個對象之間的映射。 如果第一個 Object 小於第二個 Object,我建議存儲第一個對象的信息。 否則,反其道而行之。

為清楚起見,以下是我設置這兩個對象的方式:

var firstObject = new NameValueCollection();
firstObject.Add("NameOfA", "A1");
firstObject.Add("NameOfB", "B1");
firstObject.Add("NameOfC", "C1");
firstObject.Add("NameOfD", "D1");

var secondObject = JObject.ReadFrom(tr); // tr contains your JSON

現在讓我們構建中間結構。 盡管NameValueCollection允許每個鍵存儲多個值,但我假設您只使用第一個條目。 第二步,我們需要能夠按值查找名稱。 所以,我們建立這個字典:

var keyToName = new Dictionary<string, string>();
for (int i = 0; i < firstObject.Count; ++i )
{
    keyToName.Add(firstObject.GetValues(i)[0], firstObject.GetKey(i));
}

然后我們需要從第二個 Object 中提取所有屬性。 我使用這個 function 遞歸地執行此操作:

void ProcessToken(JToken json, Dictionary<string, string> keyToName, NameValueCollection firstObject)
{
    if (json.Type == JTokenType.Property)
    {
        var prop = (JProperty)json;
        // update firstObject here
    }


    foreach (var item in json.Children())
    {
        ProcessToken(item, keyToName, firstObject);
    }
}

第一個 object 的實際更新必須放在注釋行。 在那里,我們需要檢查是否有對應於該屬性的條目。 如果我們這樣做,請更新它:

var hasKey = keyToName.TryGetValue(prop.Name, out string key);
if(hasKey)       
{
    // replace value
    firstObject.Remove(key);
    firstObject.Add(key, prop.Value.ToString());
}

就是這樣。 這是一些演示 output:

Collection before processing:
NameOfA: A1
NameOfB: B1
NameOfC: C1
NameOfD: D1

Collection after processing:
NameOfA: Value of A1
NameOfB: Value of B1
NameOfC: Value of C1
NameOfD: 100;

如果您需要其他數據結構中的最終結果,請隨意調整代碼。 核心原則始終相同。

暫無
暫無

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

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