Problem:
Can any one tell me how to return value in linq.
I want to return a collection of RadToolBarButtons and assigning them their values at the time of creation.
Code:
I tried in two ways:
IEnumerable<RadToolBarButton> collection = ContextMenuColumn.ToList()
.ForEach(x => yield return new RadToolBarButton() { Value = x });
Error 11 Cannot implicitly convert type ‘void’ to ‘System.Collections.Generic.IEnumerable’
IEnumerable<RadToolBarButton> collection =
ContextMenuColumn.SelectMany<string,IEnumerable<RadToolBarButton>>(
x => new RadToolBarButton() { Value = x });
Error 11 Cannot implicitly convert type ‘Telerik.Web.UI.RadToolBarButton’ to ‘System.Collections.Generic.IEnumerable>’. An explicit conversion exists (are you missing a cast?)
You should use
Selectinstead ofForEach, it does what you are looking for.I am not sure if the inner
ToListis necessary, you may be able to get away with aCast<T>instead of materializing the intermediate list.