Possible Duplicate:
C#: Is operator for Generic Types with inheritance
Is it possible to add a list into another list whilst changing 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));
}
Thanks.
If you want the exact equivalent:
var list = deals.Select(d => new DealBookmarkWrapper(d))
.Cast<IBookmarkWrapper>()
.ToList();
But if you're just iterating over the elements and don't really need a List
, you can leave off the call to GetList()
.
var list = deals.Select(d => new DealBookmarkWrapper(d))
.Cast<IBookmarkWrapper>()
.ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With