You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C#高效比较两数组指定属性并筛选(最小化执行时间)

Efficient C# Solution for Large Array Matching

Got it, let's fix this performance issue for you. Since you're dealing with over 2000 items in arraydata1, nested loops (like the Logic Apps foreach approach) are going to be way too slow—they run in O(n*m) time, which gets ugly fast. Instead, we can use a hash-based approach to cut this down to O(n + m) time, which is drastically more efficient.

The Core Idea

First, we extract all the tablename values from arraydata2 and store them in a HashSet<string>. Hash sets have near-instant (O(1)) lookup times, so we can quickly check if an item's name from arraydata1 matches any value in the set. Then we just filter arraydata1 once to grab all matching items.

Code Implementation

First, define strongly-typed classes to represent your data (this makes handling the arrays cleaner and avoids dynamic typing overhead):

public class ArrayDataItem1
{
    public string Id { get; set; }
    public string Name { get; set; }
}

public class ArrayDataItem2
{
    public string Id { get; set; }
    public string TableName { get; set; }
}

Then the processing logic:

// Your input arrays
var arraydata1 = new List<ArrayDataItem1>
{
    new ArrayDataItem1 { Id = "1", Name = "aaa" },
    new ArrayDataItem1 { Id = "2", Name = "bbb" },
    new ArrayDataItem1 { Id = "3", Name = "ccc" },
    new ArrayDataItem1 { Id = "4", Name = "ddd" },
    new ArrayDataItem1 { Id = "12", Name = "aaa" }
};

var arraydata2 = new List<ArrayDataItem2>
{
    new ArrayDataItem2 { Id = "111", TableName = "aaa" },
    new ArrayDataItem2 { Id = "222", TableName = "bbb" }
};

// Step 1: Build a hash set of tablenames from arraydata2
var matchingTableNames = new HashSet<string>(arraydata2.Select(item => item.TableName));

// Step 2: Filter arraydata1 to get matching items
var matchedItems = arraydata1.Where(item => matchingTableNames.Contains(item.Name)).ToList();

// Verify the result (output matches your expected format)
foreach (var item in matchedItems)
{
    Console.WriteLine($"{{ \"id\": \"{item.Id}\", \"name\": \"{item.Name}\" }}");
}

This will output exactly what you're expecting:

[ { "id": "1", "name": "aaa" }, { "id": "2", "name": "bbb" }, { "id": "12", "name": "aaa" } ]

For Logic Apps/Azure Functions (Handling JSON)

If you're running this in Logic Apps (using an Inline Code action or an Azure Function), you'll need to handle JSON serialization/deserialization. Here's how to adjust the code for that:

using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;

public static string Run(string arraydata1Json, string arraydata2Json, ILogger log)
{
    // Deserialize JSON input to strongly-typed lists
    var arraydata1 = JsonConvert.DeserializeObject<List<ArrayDataItem1>>(arraydata1Json);
    var arraydata2 = JsonConvert.DeserializeObject<List<ArrayDataItem2>>(arraydata2Json);

    // Same efficient matching logic
    var matchingTableNames = new HashSet<string>(arraydata2.Select(item => item.TableName));
    var result = arraydata1.Where(item => matchingTableNames.Contains(item.Name)).ToList();

    // Serialize the result back to JSON
    return JsonConvert.SerializeObject(result);
}

// Define the data classes with JSON property attributes to match your input keys
public class ArrayDataItem1
{
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("name")]
    public string Name { get; set; }
}

public class ArrayDataItem2
{
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("tablename")]
    public string TableName { get; set; }
}

Key Optimization Notes

  • Time Complexity: This approach runs in O(n + m) time, where n is the length of arraydata1 and m is the length of arraydata2. Compare that to nested foreach loops which run in O(n*m) time—for 2000 items in arraydata1 and 100 in arraydata2, that's 200,000 operations vs. 2100 operations. Huge difference!
  • Case Sensitivity: By default, the hash set is case-sensitive. If you need to match regardless of case (e.g., "AAA" should match "aaa"), create the hash set with a case-insensitive comparer:
    var matchingTableNames = new HashSet<string>(arraydata2.Select(item => item.TableName), StringComparer.OrdinalIgnoreCase);
    

内容的提问来源于stack exchange,提问作者Neo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:22:18