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

C# 在字典中键的值列表中插入值,c#,C#,我有一个rowsDictionary,它的键指向EmployeeSummary类的列表。 在那些EmployeeSummary类中,我们还有一个字符串属性Delivery\u System 我以这种方式循环了一遍,但是现在我想让一个deliverySystemFinder dictionanry的键是combinedKey,如下所示,每个键的值是一个不同的delivery\u system值列表 //rowsDictionary is a Dictionary<string, List&l

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

我以这种方式循环了一遍,但是现在我想让一个
deliverySystemFinder dictionanry
的键是
combinedKey
,如下所示,每个键的值是一个不同的
delivery\u 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 
    }
}
//rowsDictionary是一本字典
Dictionary deliverySystemFinder=新字典();
foreach(行dictionary.Keys中的字符串键)
{
List empList=rowsDictionary[key];
foreach(雇员摘要emp in Employist)
{
string combinedKey=emp.LastName.Trim().ToUpper()+emp.FirstName.Trim().ToUpper();
字符串交付系统=emp.delivery\u系统;
//所以现在我应该去
//A) deliverySystemFinder是否有此组合键?如果没有,请添加它。
//B) 其值列表中的combinedKey是否已经具有delivery_system的值?如果没有,则添加该值
}
}

对于start,这将起作用:

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

对于start,这将起作用:

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

谢谢“用于启动”?:)那么还有更有效的方法吗?在这段代码中,我真的需要小心内存不足的问题,这些词汇在里面就像110000个条目。问题2:是的,在实际代码中,它也有一个ID字段,所以它是三个元素的组合,为了简洁起见,我在这里发布了其中两个。问题3:哦,哇,我不知道,您建议用HashSet替换我代码中的哪些内容?@user1899082:
HashSet
是一个集合,是唯一(不同)项的无序集合(根据某些标准),适用于需要多次检查项是否属于某个集合的算法。如果您需要一个有序的项集合,包括重复项,或者不需要经常检查集合是否包含项,那么应该使用列表。有一点不清楚:您可以有多个EmployeeSummaries,具有相同的
combinedKey
s,但具有不同的
delivery\u系统
值?@user1899082:我已使用LINQ表达式更新了我的示例,该表达式应在一行中创建结果(实际上,将其拆分为多行:)。也就是说,如果我没有误解你的要求的话。谢谢。。“用于启动”?:)那么还有更有效的方法吗?在这段代码中,我真的需要小心内存不足的问题,这些词汇在里面就像110000个条目。问题2:是的,在实际代码中,它也有一个ID字段,所以它是三个元素的组合,为了简洁起见,我在这里发布了其中两个。问题3:哦,哇,我不知道,您建议用HashSet替换我代码中的哪些内容?@user1899082:
HashSet
是一个集合,是唯一(不同)项的无序集合(根据某些标准),适用于需要多次检查项是否属于某个集合的算法。如果您需要一个有序的项集合,包括重复项,或者不需要经常检查集合是否包含项,那么应该使用列表。有一点不清楚:您可以有多个EmployeeSummaries,具有相同的
combinedKey
s,但具有不同的
delivery\u系统
值?@user1899082:我已使用LINQ表达式更新了我的示例,该表达式应在一行中创建结果(实际上,将其拆分为多行:)。也就是说,如果我没有误解你的要求。