C# 替换字典值C

C# 替换字典值C,c#,.net,dictionary,C#,.net,Dictionary,我有两本字典 Dictionary<int,string> DictA=new Dictionary<int,string>(); DictA.Add(1,"A"); DictA.Add(2,"B"); DictA.Add(3,"C"); DictA.Add(4,"D"); Dictionary<string,string> DictB=new Dictionary<string,string>(); DictB.Add("A","A1"); Di

我有两本字典

Dictionary<int,string> DictA=new Dictionary<int,string>();
DictA.Add(1,"A");
DictA.Add(2,"B");
DictA.Add(3,"C");
DictA.Add(4,"D");
Dictionary<string,string> DictB=new Dictionary<string,string>();
DictB.Add("A","A1");
DictB.Add("C","C1");
DictB.Add("D","D1");

现在我需要一个结果字典,如下所示

//因为B在第二本字典中丢失了,所以它应该 保留原有价值
我对使用foreach为此编写的迭代器代码不满意,但我确信使用Lambda表达式有一种非常简洁的方法可以做到这一点。我对他们很陌生,发现他们很难理解

有人能解释一个好的资源来学习它们并通过一个例子有效地使用它们吗。

你可以使用LINQ。基本上只需检查第二个字典是否包含第一个字典的值作为键,如果它确实从第二个字典中获取值,如果它没有从第一个字典中获取值:

DictA
.Select(x => new KeyValuePair<int,string>(x.Key, 
                                          DictB.ContainsKey(x.Value) 
                                          ? DictB[x.Value] 
                                          : x.Value)
.ToDictionary(x => x.Key, x => x.Value);
您可以使用生成所需的词典

var result = DictA.ToDictionary(a => a.Key, a => DictB.ContainsKey(a.Value) ? DictB[a.Value] : a.Value);

foreach (var item in result)
{
    Console.WriteLine(item.Key + ", " + item.Value);
}
结果如下

1,A1 2,B 3,C1 第4条,首被告

如果你想在原地做,你可以修改格言

非常简单:

var result =
    DictA
        .ToDictionary(
            a => a.Key,
            a => DictB.ContainsKey(a.Value)
                ? DictB[a.Value]
                : a.Value);
我得到这个结果:

既然你想在原地做


这里有一种方法可以满足您的需求:

public static Dictionary<TKey,TValue> Extend<TKey,TValue>( params Dictionary<TKey,TValue>[] sources )
{
  return Extend<TKey,TValue>( null , sources ) ;
}

public static Dictionary<TKey,TValue> Extend<TKey,TValue>( IEqualityComparer<TKey> comparer , params Dictionary<TKey,TValue>[] sources )
{
  var result = new Dictionary<TKey,TValue>( comparer ?? EqualityComparer<TKey>.Default ) ;

  foreach( var src in sources )
  {
    if ( src == null ) throw new ArgumentNullException("sources", "source dictionary may not be null");

    foreach( var item in src )
    {
      result[item.Key] = item.Value ;
    }
  }

  return result ;
}
使用Linq还有另一种方法:

public static Dictionary<TKey,TValue> Extend<TKey,TValue>( params Dictionary<TKey,TValue>[] sources )
{
  return Extend<TKey,TValue>( null , sources ) ;
}
public static Dictionary<TKey,TValue> Extend<TKey,TValue>( IEqualityComparer<TKey> comparer , params Dictionary<TKey,TValue>[] sources )
{
  return sources
         .SelectMany( kvp => kvp )
         .GroupBy( kvp => kvp.Key , kvp => kvp.Value , comparer )
         .ToDictionary( grp => grp.Key , grp => grp.Last() , comparer )
         ;
}

但是我敢打赌,第一个简单的方法会更快。

我对我编写的迭代器代码不满意,你应该给我们看看代码。这可能是最好的方法。那是在我的剪贴板上!Select then ToDictionary调用是不必要的。OP不希望将其放在适当的位置,现在我需要一个如下所示的结果字典现在我需要一个如下所示的结果字典,并且它位于内容中。我们应该相信谁?标题还是内容D
public static Dictionary<TKey,TValue> Extend<TKey,TValue>( params Dictionary<TKey,TValue>[] sources )
{
  return Extend<TKey,TValue>( null , sources ) ;
}

public static Dictionary<TKey,TValue> Extend<TKey,TValue>( IEqualityComparer<TKey> comparer , params Dictionary<TKey,TValue>[] sources )
{
  var result = new Dictionary<TKey,TValue>( comparer ?? EqualityComparer<TKey>.Default ) ;

  foreach( var src in sources )
  {
    if ( src == null ) throw new ArgumentNullException("sources", "source dictionary may not be null");

    foreach( var item in src )
    {
      result[item.Key] = item.Value ;
    }
  }

  return result ;
}
public static Dictionary<TKey,TValue> Extend<TKey,TValue>( params Dictionary<TKey,TValue>[] sources )
{
  return Extend<TKey,TValue>( null , sources ) ;
}
public static Dictionary<TKey,TValue> Extend<TKey,TValue>( IEqualityComparer<TKey> comparer , params Dictionary<TKey,TValue>[] sources )
{
  return sources
         .SelectMany( kvp => kvp )
         .GroupBy( kvp => kvp.Key , kvp => kvp.Value , comparer )
         .ToDictionary( grp => grp.Key , grp => grp.Last() , comparer )
         ;
}