2010-10-13 6 views

Répondre

7

Utilisez SelectMany et ont le retourner une liste concaténée de la PrimaryItem et OtherItems (si elles sont présentes):

var result = fooList 
    .SelectMany(f => (new[] { f.PrimaryItem }) 
     .Concat(f.HasOtherItems ? f.OtherItems : new int[] { })) 
    .Distinct(); 
1

Comme une légère variation:

var items = fooList.Select(i => i.PrimaryItem).Union(
     fooList.Where(foo => foo.HasOtherItems).SelectMany(foo => foo.OtherItems)); 

Cela prend la ensemble de PrimaryItem, et (où HasOtherItems est défini) concatène l'ensemble combiné de OtherItems. Le Union assure distinct.

+0

bonne solution :) – fearofawhackplanet