Using LINQ to see if Item already exists in listview

  • Thread starter Thread starter Gaz
  • Start date Start date
G

Gaz

Im trying to use c# and linq to simplfy searching through a listview
but cannot get it to work can anyone help?


here is what i got so far which doesnt work.



var qry = from L in listviewname.items.all

where l.item.value == "123"

Select L.item.value;



Thanks Gary
 
Gaz said:
Im trying to use c# and linq to simplfy searching through a listview
but cannot get it to work can anyone help?


here is what i got so far which doesnt work.

var qry = from L in listviewname.items.all
where l.item.value == "123"
Select L.item.value;

Okay, a few things:

1) C# is case-sensitive; l isn't the same as L, and Select isn't the
same as select; items isn't the same as Items

2) What do you expect the "all" property to do?

3) What do you expect the L range variable to be? It should be just
"object" but you're trying to use an "item" property in it. If you
know a particular type, state it in the "from" clause. (Only
required for weakly typed collections like this.)

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
ok i will start again...

Im trying to learn LINQ and im not sure if im going about this in the
correct method.

I want to query all the items in the listview collection where the
item key value in the collection is equal to what i tell it..

This is my code..

var qry = from itm in uListSelected.Items
where itm.key == "1"
Select itm.key;

foreach (var Result in qry)
{
return true;
}

Is this bit more helpful?
 
its to replace this


foreach(Infragistics.Win.UltraWinListView.UltraListViewItem Itm in
uListSelected.Items)
{

if (KeyID == Convert.ToString(Itm.Key))
{
return true;

}

}
 
Gaz said:
Im trying to learn LINQ and im not sure if im going about this in the
correct method.

I want to query all the items in the listview collection where the
item key value in the collection is equal to what i tell it..

This is my code..

var qry = from itm in uListSelected.Items
where itm.key == "1"
Select itm.key;

That won't compile due to:

1) You've still got an untyped collection
2) You're using "Select" instead of "select"

If it did compile, it would just give you a bunch of strings, each of
which is "1" - you've selected the value that you've just tested. You
probably want "select itm" instead of "select itm.key".
foreach (var Result in qry)
{
return true;
}

Is this bit more helpful?

Getting there. I still don't know what type of object you've actually
got in your list though.

How confident are you in C# 1 and 2? I'd make sure you understand those
thoroughly before learning LINQ, personally.
 
Gaz said:
foreach(Infragistics.Win.UltraWinListView.UltraListViewItem Itm in
uListSelected.Items)
{

if (KeyID == Convert.ToString(Itm.Key))
{
return true;

}

}

If you're just trying to find if *anything* matches the key, something
like:

return uListSelected.Items.Cast<UltraListViewItem>.Any
(item => item.Key==KeyID)

Note that (assuming the docs I'm looking at are correct) there's no
point using Convert.ToString on the Key property, as it's already a
string.
 
If you're just trying to find if *anything* matches the key, something
like:

return uListSelected.Items.Cast<UltraListViewItem>.Any
(item => item.Key==KeyID)

Note that (assuming the docs I'm looking at are correct) there's no
point using Convert.ToString on the Key property, as it's already a
string.

Thanks for you help but im still lost so ill think ill stick to what i
know for the time being and stop trying to use to stuff

If i dont convert.tostring the key i get this warning message..

Warning 1 Possible unintended reference comparison; to get a value
comparison, cast the right hand side to type 'string'
 
Thanks for you help but im still lost so ill think ill stick to what i
know for the time being and stop trying to use to stuff

If you're reasonably new to C#, I'd definitely steer clear of LINQ just
for the moment. It's a *fantastic* technology, but you need to be
confident in the foundations first.
If i dont convert.tostring the key i get this warning message..

Warning 1 Possible unintended reference comparison; to get a value
comparison, cast the right hand side to type 'string'

That suggests you're not using the type I thought you were... I was
looking at this documentation:

http://help.infragistics.com/Help/NetAdvantage/NET/2007.3/CLR2.0/html/I
nfragistics2.Win.UltraWinListView.v7.3
~Infragistics.Win.UltraWinListView.UltraListViewItem~Key.html

which suggests that the Key property is of type "string". If you hover
over the use of the Key property in Visual Studio, what does it say?
 

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

Similar Threads


Back
Top