需要按c#中的键和值交叉2个词典

时间:2016-04-21 08:31:42

标签: c# .net linq

我有2个结构:

public struct Customer
{
    public string Code { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
}

public class OrganizationDto
{
    public int OrgId { get; set; }
    public string Name { get; set; }
    public int PeopleCount { get; set; }
    public string CCEmail { get; set; }
    public string address { get; set; }
}

和2个词典:

Dictionary<string, Customer> dataCustomer = new Dictionary<string, Customer>();
Dictionary<string, OrganizationDto> dataOrganization = new Dictionary<string, OrganizationDto>();

如何通过以下方式映射2:
关键和不同的地址。所以我需要具有相同键但具有不同地址的物品 我试过了:

Dictionary<string, OrganizationDto> changed = dataOrganization
            .Where(item => dataCustomer .Keys.Contains(item.Key))
            .ToDictionary(item => item.Key, item => item.Value);

这给了我按键的交集,但我不知道如何只选择具有不同地址的那些(当然还有普通键)。

由于

1 个答案:

答案 0 :(得分:2)

过滤时比较地址属性

var changed = dataOrganization
        .Where(item => dataCustomer.Keys.Contains(item.Key) 
                       && item.Address != dataCustomer[item.Key].Address)
        .ToDictionary(item => item.Key, item => item.Value);