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.
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().
List
GetList()
var list = deals.ConvertAll(item=>new DealBookmarkWrapper(item));
" ", :
var list = new List<IBookmarkWrapper>(); //already existing ... deals.Aggregate(list, (s, c) => { s.Add(new DealBookmarkWrapper(c)); return s; });