1

I am currently studying Linq to filtering data from Listview

for example , I have Listview table

enter image description here

I would like to get data which Emp Name is Rob BOB and Department is Developer

In order to make sure Linq is working, I was trying to find Rob BOB in listview table

I was trying

        Dim Conflicts = From ListItem As ListViewItem In ListView1.Items
                       Select "Rob BOB" = ListItem.Text

and

        Dim Conflicts As IEnumerable(Of ListView) = From item In ListView1.Items
        Where item.SubItems(1).Text = "Rob BOB"
                                    Select item.Text

 For Each ConflictedGroup In Conflicts
        MsgBox(ConflictedGroup.ToString)
    Next

to get a data

However, it doesn't return anything or giving exception error

i was checking

How do I use LINQ to get all the Items that have a particular SubItem?

to follow how to use linq for Listview, but doesn't work ..

Does anybody know how to use Linq for Listview ?

thanks

1 Answer 1

2

For one thing, item.SubItems(1).Text = "Rob BOB" is incorrect. The Item.Text maps to SubItems(0). SubItems(1) would be the address. So that bit is searching the Address "column" for the name and should return nothing.

enter image description here

This works:

Dim dupes = myLV.Items.Cast(Of ListViewItem).
        Where(Function(w) w.Text.ToLowerInvariant = "ziggy jones").
        Select(Function(s) s.Text).ToList

For Each s As String In dupes
    Console.WriteLine(s)
Next

Output:

Ziggy Jones
Ziggy Jones

Your second block would work like so:

Dim Conflicts = From ListItem In myLV.Items.Cast(Of ListViewItem)()
                Where ListItem.Text = "Ziggy Jones"
                Select ListItem.Text

It seems like it might be more useful to collect the indicies of the dupes.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your help. I tried second option t getting data however, it seems "Conflicts" variable doesn't get any results from Linq statement. Is there anyway to check data in the column just like sql ? (or item.SubItems(1) is only way to access data in listview? )
Conflicts ends up with 2 items for me. Maybe edit your post with the revised attempt. Also, as I said, a simple List of the names that are dupes doesnt seem very useful - you'd have to find them again to do anything about it.
Also, rather than a ListView, if the data was in a List(Of MyEmpClass) you could bind it to a DGV without making copies of the data. Then you could query, sort, filter and prune the list much more easily.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.