VB.NET使用Linq从Listview中获取数据[英] VB.NET using Linq to get data from Listview

本文是小编为大家收集整理的关于VB.NET使用Linq从Listview中获取数据的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我目前正在研究 Linq 从 Listview 过滤数据

例如,我有 Listview 表

在此处输入图片描述

我想获取 Emp Name 为 Rob BOB 且 Department 为 Developer 的数据

为了确保 Linq 正常工作,我试图在 listview 表中找到 Rob BOB

我在尝试

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

        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

获取数据

但是,它不返回任何内容或给出异常错误

我在检查

如何使用 LINQ 获取具有特定 SubItem 的所有项目?

了解如何将 linq 用于 Listview,但不起作用..

有人知道如何将 Linq 用于 Listview 吗?

谢谢

推荐答案

一方面,item.SubItems(1).Text = "Rob BOB" 不正确.Item.Text 映射到 SubItems(0).SubItems(1) 将是地址.因此,该位正在地址"列"中搜索名称,并且不应返回任何内容.

在此处输入图片描述

这行得通:

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

输出:

<块引用>

齐吉·琼斯
齐吉·琼斯

你的第二个区块会这样工作:

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

似乎收集受骗者的指标可能更有用.

本文地址:https://www.itbaoku.cn/post/1557015.html

问题描述

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

推荐答案

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.