是否有可能重现这种说法不使用foreach? [英] Is it possible to recreate this statement without using a foreach?

查看:133
本文介绍了是否有可能重现这种说法不使用foreach?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/11186493/c-is-operator-for-generic-types-with-inheritance">C#:是运营商泛型类型继承

是否有可能增加一个列表到另一个列表,同时改变类类型的交易,而无需使用foreach语句来DealBookmarkWrapper?

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));
}

感谢。

推荐答案

如果你想完全等效:

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

但如果你只是遍历元素并不真的需要一个列表,你可以离开过调用的GetList( )

But if you're just iterating over the elements and don't really need a List, you can leave off the call to GetList().

这篇关于是否有可能重现这种说法不使用foreach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆