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 Select instead of ForEach, it does what you are looking for.
IEnumerable<RadToolBarButton> collection = ContextMenuColumn.ToList()
.Select(x => new RadToolBarButton { Value = x });
I am not sure if the inner ToList is necessary, you may be able to get away with a Cast<T> instead of materializing the intermediate list.
Use Select instead of ForEach, it will do the yield return for you
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