Is it possible to recreate this statement without using foreach?

Possible duplicate:
C #: Is the operator for generic types with inheritance

Is it possible to add a list to another list by changing the class type from Deal to DealBookmarkWrapper without using the foreach statement?

var list = new List<IBookmarkWrapper>();
foreach (var deal in deals)
{
    list.Add(new DealBookmarkWrapper(deal));
}

Thank.

+5
source share
4 answers

If you need the exact equivalent:

var list = deals.Select(d => new DealBookmarkWrapper(d))
                .Cast<IBookmarkWrapper>()
                .ToList();

But if you just iterate over the elements and don't need to List, you can leave a challenge GetList().

+9
source
var list = deals.Select(d => new DealBookmarkWrapper(d))
                .Cast<IBookmarkWrapper>()
                .ToList();
+4
source

 var list = deals.ConvertAll(item=>new DealBookmarkWrapper(item)); 
+3

" ", :

var list = new List<IBookmarkWrapper>();  //already existing
...  
deals.Aggregate(list, (s, c) => 
                      { 
                        s.Add(new DealBookmarkWrapper(c)); 
                        return s; 
                      });
+1

All Articles