Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Using LINQ to see if Item already exists in listview

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gaz - 19 Mar 2008 12:10 GMT
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
Jon Skeet [C# MVP] - 19 Mar 2008 12:19 GMT
>  Im trying to use c# and linq to simplfy searching through a listview
> but cannot get it to work can anyone help?
[quoted text clipped - 4 lines]
>              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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Gaz - 19 Mar 2008 13:13 GMT
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?
Gaz - 19 Mar 2008 13:18 GMT
its to replace this

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

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

               }

           }
Jon Skeet [C# MVP] - 19 Mar 2008 13:52 GMT
> foreach(Infragistics.Win.UltraWinListView.UltraListViewItem Itm in
> uListSelected.Items)
[quoted text clipped - 7 lines]
>
>             }

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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Gaz - 19 Mar 2008 14:15 GMT
> > foreach(Infragistics.Win.UltraWinListView.UltraListViewItem Itm in
> > uListSelected.Items)
[quoted text clipped - 21 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

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'
Jon Skeet [C# MVP] - 19 Mar 2008 14:28 GMT
<snip>

> 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?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Jon Skeet [C# MVP] - 19 Mar 2008 13:25 GMT
> Im trying to learn LINQ and im not sure if im going about this in the
> correct method.
[quoted text clipped - 7 lines]
>                       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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.