I have the following piece of code:
// Iterate through the root menu items in the Items collection.
foreach (MenuItem item in NavigationMenu.Items)
{
if (item.NavigateUrl.ToLower() == ThisPage.ToLower())
{
item.Selected = true;
}
}
What I’d like is:
var item = from i in NavigationMenu.Items
where i.NavigateUrl.ToLower() == ThisPage.ToLower()
select i;
Then I can set the Selected value of item, but it gives me an error on the NavigationMenu.Items.
Error 5 Could not find an implementation of the query pattern for
source type ‘System.Web.UI.WebControls.MenuItemCollection’. ‘Where’
not found. Consider explicitly specifying the type of the range
variable ‘i’.
When I comment out the where clause, I get this error:
Error 22 Could not find an implementation of the query pattern for
source type ‘System.Web.UI.WebControls.MenuItemCollection’. ‘Select’
not found. Consider explicitly specifying the type of the range
variable ‘i’.
I suspect
NavigationMenu.Itemsonly implementsIEnumerable, notIEnumerable<T>. To fix this, you probably want to callCast, which can be done by explicitly specifying the element type in the query:However, your query is named misleadingly – it’s a sequence of things, not a single item.
I’d also suggest using a
StringComparisonto compare the strings, rather than upper-casing them. For example:I’d then consider using extension methods instead: