Need noob help with LINQ

T

Tom P.

I'm not getting the theory behind LINQ, and without that I can't get
my head around how to use it and why.

Here's what I _think_ I'm trying to do:
I've got a menu with several items. Some other part of the application
is changing the text in a combobox and I want to set the indicator to
the correct menuitem. I can loop through the Items collection and look
at the Tag for my indicator, like so:
foreach (ToolStripItem CurrentButton in
_contextMenuStripFavorites.Items)
{
if (CurrentButton.Text == "")
continue;

string ItemPath = (string)CurrentButton.Tag;

if (ItemPath == FavoritePath)
{
base.Text = CurrentButton.Text;
break;
}

}

But I think using LINQ would be a better solution.

My first question is, would using LINQ actually be a better solution?
Isn't LINQ just going to loop like I would?

My second question is why doesn't this code work?

ToolStripItem CurrentItem;

var favoriteItem =
from ToolStripItem CurrentButton in
_contextMenuStripFavorites.Items
where ((string)Tag) == FavoritePath
select CurrentButton;

CurrentItem = (ToolStripItem)favoriteItem;
base.Text = CurrentItem.Text;

More to the point, why doesn't the LINQ part work and what's wrong
with it?
After several attempts to get it to compile I finally ran it and got
the following error:

" Unable to cast object of type '<WhereIterator>d__0`
1[System.Windows.Forms.ToolStripItem]' to type
'System.Windows.Forms.ToolStripItem' "

Why can't it cast something to the same type? Why is it casting
anything? What am I doing wrong? I need to understand this stuff but
these errors are not helping.

Thanks for the help... and the patience.
Tom P.
 
T

Tom P.

OK, I got the following code to execute without thowing an exception:
ToolStripMenuItem CurrentItem;

var favoriteItem =
from ToolStripItem CurrentButton in
_contextMenuStripFavorites.Items
where ((string)CurrentButton.Tag) == FavoritePath
select CurrentButton;

CurrentItem = favoriteItem as ToolStripMenuItem;

....but I can't "find" the results. How do I get the results out? I can
see it in the debugger, but I can't see how to get it out. Do I just
use an indexer?

CurrentItem = favoriteItem[0] as ToolStripMenuItem;

Is it really that easy? The results will always only be one item. Does
LINQ always return an array?

Tom P.
 
M

Martin Honnen

Tom said:
OK, I got the following code to execute without thowing an exception:
ToolStripMenuItem CurrentItem;

var favoriteItem =
from ToolStripItem CurrentButton in
_contextMenuStripFavorites.Items
where ((string)CurrentButton.Tag) == FavoritePath
select CurrentButton;

CurrentItem = favoriteItem as ToolStripMenuItem;

...but I can't "find" the results. How do I get the results out? I can
see it in the debugger, but I can't see how to get it out. Do I just
use an indexer?

CurrentItem = favoriteItem[0] as ToolStripMenuItem;

Is it really that easy? The results will always only be one item.

Use
CurrentItem = favoriteItem.FirstOrDefault();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top