Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
linq中的收益率_Linq_Foreach_Return_Yield - Fatal编程技术网

linq中的收益率

linq中的收益率,linq,foreach,return,yield,Linq,Foreach,Return,Yield,问题: 有人能告诉我如何在linq中返回值吗。 我想返回一组RadToolbar按钮,并在创建时为它们赋值 代码: 我试过两种方法: IEnumerable<RadToolBarButton> collection = ContextMenuColumn.ToList() .ForEach(x => yield return new RadToolBarButton() { Value = x }); IEnumerable collection=ContextM

问题: 有人能告诉我如何在linq中返回值吗。 我想返回一组RadToolbar按钮,并在创建时为它们赋值

代码: 我试过两种方法:

IEnumerable<RadToolBarButton> collection = ContextMenuColumn.ToList()
      .ForEach(x => yield return new RadToolBarButton() { Value = x });
IEnumerable collection=ContextMenuColumn.ToList()
.ForEach(x=>yield返回新的radtoolbutton(){Value=x});
错误11无法将类型“void”隐式转换为“System.Collections.Generic.IEnumerable”

IEnumerable<RadToolBarButton> collection =
    ContextMenuColumn.SelectMany<string,IEnumerable<RadToolBarButton>>(
       x => new RadToolBarButton() { Value = x });
IEnumerable集合=
ContextMenuColumn.SelectMany(
x=>new RadToolBarButton(){Value=x});

错误11无法将类型“Telerik.Web.UI.RadToolBarButton”隐式转换为“System.Collections.Generic.IEnumerable>”。存在显式转换(您是否缺少转换?

使用
Select
而不是
ForEach
,它将为您执行
收益率调整

您应该使用
Select
而不是
ForEach
,它可以完成您需要的操作

IEnumerable<RadToolBarButton> collection = ContextMenuColumn.ToList()
  .Select(x => new RadToolBarButton { Value = x });
IEnumerable collection=ContextMenuColumn.ToList()
.Select(x=>newradtoolbarbutton{Value=x});
我不确定是否需要内部的
ToList
,您可以通过
Cast
而不是具体化中间列表