Skip to main content
 首页 » 编程设计

c#之在字典中键的值列表中插入值

2024年02月06日13jyk

我有一个 rowsDictionary,它的键指向 EmployeeSummary 类的列表。 在那些 EmployeeSummary 类中,我们还有一个字符串属性 Delivery_System

我正在以这种方式循环,但现在停留在我想要一个 deliverySystemFinder dictioanry 的部分,它的键是 combinedKey 如下所示,值是每个键都是不同的 delivery_system 值的列表

//rowsDictionary is a Dictionary<string, List<EmployeeSummary>> 
 
Dictionary<string, List<string>> deliverySystemFinder = new Dictionary<string, List<string>>(); 
 
foreach (string key in rowsDictionary.Keys) 
{ 
    List<EmployeeSummary> empList = rowsDictionary[key]; 
    foreach (EmployeeSummary emp in empList) 
    { 
        string combinedKey = emp.LastName.Trim().ToUpper() + emp.FirstName.Trim().ToUpper(); 
        string delivery_system = emp.Delivery_System; 
        // so now I should go and  
        //A) does deliverySystemFinder have this combinedKey? if not add it.  
        //B) Does combinedKey in the list of its values already have the value for delivery_system? if it does not then add it  
    } 
} 

请您参考如下方法:

这会起作用,一开始:

foreach (string key in rowsDictionary.Keys) 
{ 
    List<EmployeeSummary> empList = rowsDictionary[key]; 
    foreach (EmployeeSummary emp in empList) 
    { 
        string combinedKey = emp.LastName.Trim().ToUpper() + 
            emp.FirstName.Trim().ToUpper(); 
 
        string delivery_system = emp.Delivery_System; 
 
        List<string> systems = null; 
 
        // check if the dictionary contains the list 
        if (!deliverySystemFinder.TryGetValue(combinedKey, out systems)) 
        { 
            // if not, create it and add it  
            systems = new List<string>(); 
            deliverySystemFinder[combinedKey] = systems; 
        } 
 
        // check if the list contains the value and add it 
        if (!systems.Contains(delivery_system)) 
            systems.Add(delivery_system); 
    } 
} 

现在,几点说明:

  1. 遍历 Keys 没有意义,然后在每次迭代中进行查找。可以直接迭代KeyValuePair使用 foreach循环。

  2. 使用连接字符串作为唯一键通常会失败。在这种情况下,如果您有用户 { LastName="Some", FirstName="Body" } 会怎样?和 { LastName="So", FirstName="Mebody" }在你的列表中?

  3. 检查是否 List包含一个值是 O(n)手术。如果使用 HashSet<string>,您将大大提高性能相反。

最后,实现您想要做的事情的最简单方法是放弃这些循环并简单地使用:

// returns a Dictionary<EmployeeSummary, List<string>> 
// which maps each distinct EmployeeSummary into a list of 
// distinct delivery systems 
var groupByEmployee = rowsDictionary 
    .SelectMany(kvp => kvp.Value) 
    .GroupBy(s => s, new EmployeeSummaryEqualityComparer()) 
    .ToDictionary( 
         s => s.Key,  
         s => s.Select(x => x.Delivery_System).Distinct().ToList()); 

与 EmployeeSummaryEqualityComparer定义如下:

class EmployeeSummaryEqualityComparer : IEqualityComparer<EmployeeSummary> 
{ 
    public bool Equals(EmployeeSummary x, EmployeeSummary y) 
    { 
        if (object.ReferenceEquals(x, null)) 
            return object.ReferenceEquals(y, null); 
 
        return  
            x.FirstName == y.FirstName && 
            x.LastName == y.LastName && 
            ... (depending on what constitutes 'equal' for you) 
    } 
 
    public int GetHashCode(EmployeeSummary x) 
    { 
        unchecked 
        { 
            var h = 31;   // null checks might not be necessary? 
            h = h * 7 + (x.FirstName != null ? x.FirstName.GetHashCode() : 0); 
            h = h * 7 + (x.LastName != null ? x.LastName.GetHashCode() : 0); 
            ... other properties similarly ... 
            return h; 
        } 
    } 
} 

如果你真的认为使用 string key 将适用于您的所有情况,您可以在没有自定义相等比较器的情况下执行此操作:

// returns a Dictionary<string, List<string>> 
var groupByEmployee = rowsDictionary 
    .SelectMany(kvp => kvp.Value) 
    .GroupBy(s => s.LastName.ToUpper() + s.FirstName.ToUpper()) 
    .ToDictionary( 
         s => s.Key, 
         s => s.Select(x => x.Delivery_System).Distinct().ToList());