2016-04-08 4 views
0

私はそれがこのようになります辞書にliquを使用してSQL結果を変換しています、簡単な質問を持っている:(辞書用c#linquと辞書には、キーがprocesに存在するかどうかを確認できますか?

  result.ExtendedRelationshipSet = reader.Read().AsParallel() 
                  .Select(r => 
        new RelationshipsSyncComp.RelationshipCollection.ExtendedRelationship() 
      { 
       Id = r.RelationshipId, 
       FromItemId = r.fromDocumentId, 
       FromItem = new Item() {Id = r.fromDocumentId}, 
       FromBridgeId = r.fromBridgeId ?? r.fromDocumentId, 
       FromDocumentKey = r.fromDocumentKey, 
      }).ToDictionary(x => RelationshipsSyncComp.RelationshipCollection 
                 .RelationshipResultKey(type, x), x => x); 

キーは、IDからの組み合わせからとidに構築ですが、私はユニークな存在ではない気づきましたdb内のレコードが重複するトランザクションバグ)、基本的に重複エントリがあります。

辞書内のキーが既に存在する場合は上記の手順を使用してチェックしたり、重複するキーを無視したりすることはできますか?

+0

希望するキー(データを照会した後)でグループ化して辞書を作成すると、大丈夫でしょうか? – Fredrik

答えて

2

Lookupを使用すると、重複するキーを許可できます。あなたは重複を無視したい場合は、あなたがmorelinqからToDictionary前DistinctByを使用するか、または独自の拡張

public static IEnumerable<TSource> DistinctBy<TSource, TKey> 
    (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) 
{ 
    HashSet<TKey> keys = new HashSet<TKey>(); 
    foreach (TSource item in source) 
     if (keys.Add(keySelector(element))) 
      yield return item ; 
} 

を作成し、前にそれを呼び出すことができ、代わりにToDictionary()

ToLookupメソッドを使用ToDictionary

 result.ExtendedRelationshipSet = reader.Read().AsParallel() 
                 .Select(r => 
       new RelationshipsSyncComp.RelationshipCollection.ExtendedRelationship() 
     { 
      Id = r.RelationshipId, 
      FromItemId = r.fromDocumentId, 
      FromItem = new Item() {Id = r.fromDocumentId}, 
      FromBridgeId = r.fromBridgeId ?? r.fromDocumentId, 
      FromDocumentKey = r.fromDocumentKey, 
     }) 
     .DistinctBy(x=> RelationshipsSyncComp.RelationshipCollection.RelationshipResultKey(type, x)) 
     .ToDictionary(x => RelationshipsSyncComp.RelationshipCollection.RelationshipResultKey(type, x), x => x); 
1

オプション1 - キーが重複している場合は、最初の項目を選択します。

 result.ExtendedRelationshipSet = reader.Read().AsParallel().Select(r => new RelationshipsSyncComp.RelationshipCollection.ExtendedRelationship() 
     { 
      Id = r.RelationshipId, 
      FromItemId = r.fromDocumentId, 
      FromItem = new Item() {Id = r.fromDocumentId}, 
      FromBridgeId = r.fromBridgeId ?? r.fromDocumentId, 
      FromDocumentKey = r.fromDocumentKey, 
     }).GroupBy(g=>RelationshipsSyncComp.RelationshipCollection.RelationshipResultKey(type, x)) 
      .ToDictionary(x => x.Key, x => x.First()); 

オプション2 - 重複した項目をフィルタリングまたは無視する。Key

 result.ExtendedRelationshipSet = reader.Read().AsParallel().Select(r => new RelationshipsSyncComp.RelationshipCollection.ExtendedRelationship() 
     { 
      Id = r.RelationshipId, 
      FromItemId = r.fromDocumentId, 
      FromItem = new Item() {Id = r.fromDocumentId}, 
      FromBridgeId = r.fromBridgeId ?? r.fromDocumentId, 
      FromDocumentKey = r.fromDocumentKey, 
     }).GroupBy(g=>RelationshipsSyncComp.RelationshipCollection.RelationshipResultKey(type, x)) 
      .Where(e=>e.Count() > 1) // ignore duplicates 
      .ToDictionary(x => x.Key, x => x.First()); 
0

あなたは.ToDictionary(前.GroupByを())を使用する場合、あなたが値としてリストと辞書を作成することができます。

関連する問題