2016-03-23 11 views
1

>() Listeye dönüştürme Linq ile bu listeyi dönüştürmek mümkün mü? Linq kullanarak peşin Hiyerarşik Sözlük Ben şu anda ben foreach kullanıyorum</p> <p>düz sistem Sözlük için <code>List<KeyValuePair<Document, File>></code> olanı dönüştürmek istediğiniz <code>Dictionary<Document, File[ n ]></code> gibi bir Sözlüğü var

+0

'foreach' ->' Select'. 'foreach foreach' -> SelectMany' –

+1

Neden Linq kullanmak istiyorsun? Kodunda bir sorun mu var? Büyük/büyük veri depolama için döngü için basit basit çok daha hızlı sonra Linq çözümdür. –

+0

@MaciejLos Hayır Kod mükemmel çalışıyor sadece kod satırını azaltmak için Linq'de dönüştürmek istedim –

cevap

3

yılında

 var documentFileList = RepositoryFactory.FileRepository.GetFiles(_case); 

    var retVal = new List<KeyValuePair<Document, File>>(); 
    if (documentFileList != null) 
    { 
    foreach (var docFileKeyPair in documentFileList) 
    { 
     if (docFileKeyPair.Value != null) 
     { 
     foreach (var fileEntity in docFileKeyPair.Value) 
     { 
      var document = docFileKeyPair.Key as Document; 
      if (document != null) 
      { 
      retVal.Add(new KeyValuePair<Document, File>(document, fileEntity)); 
      } 
     } 
     } 
    } 
    } 

sayesinde, bu yapabilirdi.

var flattenList = documentFileList.SelectMany(x=>x.Value.Where(v=> v != null).Select(s=> new KeyValuePair<Document, File>(x.Key, s))) 
            .ToList(); 

Kontrol bu example code

+0

OP kodu 'null' kontrolleri dahil nasıl. –

+0

@IvanStoev Güzel yakalama, cevabımı güncelledim. –

+0

Teşekkürler @HariPrasad –

İlgili konular