问题:有人能告诉我如何在linq中返回值吗?我想返回一个RadToolBarButtons集合,并在创建时为它们赋值。
代码:我尝试了两种方式:
IEnumerable<RadToolBarButton> collection = ContextMenuColumn.ToList()
.ForEach(x => yield return new RadToolBarButton() { Value = x });错误11无法将类型“void”隐式转换为“System.Collections.Generic.IEnumerable”
IEnumerable<RadToolBarButton> collection =
ContextMenuColumn.SelectMany<string,IEnumerable<RadToolBarButton>>(
x => new RadToolBarButton() { Value = x });错误11无法将类型'Telerik.Web.UI.RadToolBarButton‘隐式转换为'System.Collections.Generic.IEnumerable>’。存在显式转换(是否缺少强制转换?)
发布于 2012-08-05 19:00:02
你应该使用Select而不是ForEach,它能做你想要的事情。
IEnumerable<RadToolBarButton> collection = ContextMenuColumn.ToList()
.Select(x => new RadToolBarButton { Value = x });我不确定内部ToList是否是必要的,您也许可以使用Cast<T>而不是物化中间列表。
发布于 2012-08-05 18:59:59
使用Select而不是ForEach,它将为您执行yield returm
https://stackoverflow.com/questions/11815841
复制相似问题