Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq在c中从ListViewItemCollections中选择项_C#_Linq - Fatal编程技术网

C# Linq在c中从ListViewItemCollections中选择项

C# Linq在c中从ListViewItemCollections中选择项,c#,linq,C#,Linq,如何在C中使用Linq从ListViewItemCollections中选择ListViewItem 我试过用这个,但没用 ListViewItemCollections lv = listview1.items; var test = from xxx in lv where xxx.text = "data 1" select xxx; test <--- now has the listviewitem with "data 1" as string value.. 要获

如何在C中使用Linq从ListViewItemCollections中选择ListViewItem

我试过用这个,但没用

ListViewItemCollections lv = listview1.items;

var test = from xxx in lv where xxx.text = "data 1" select xxx;


test   <--- now has the listviewitem with "data 1" as string value..
要获取ListViewItem的枚举数,必须强制转换ListView的Items集合:

IEnumerable<ListViewItem> lv = listview1.items.Cast<ListViewItem>();
ListViewItemCollection只实现IEnumerable而不是IEnumerable,因此编译器无法推断xxx的类型,并且LINQ查询不起作用

您需要强制转换集合,以便像这样使用它

var test = from xxx in lv.Cast<ListViewItem>() where xxx.text="data 1" select xxx;


如果要将搜索限制为一列,可以使用

IEnumerable<ListViewItem> lv = listView.Items.Cast<ListViewItem>();

var rows =  from x in lv
            where x.SubItems[columnIndex].Text == searchTerm
            select x;

if (rows.Count() > 0)
{
    ListViewItem row = rows.FirstOrDefault();

    //TODO with your row
}

我认为您应该使用:listview1.items.OfType;为了避免可能的强制转换异常@Preet Sangha:我认为不需要它,因为添加到ListView的项始终能够强制转换到ListViewItem
ListViewItemCollections lv = listview1.items;

var test = from ListViewItem xxx in lv where xxx.text == "data 1" select xxx;
listview1.items.Cast<ListViewItem>().Where(i => i.text == "date 1");
IEnumerable<ListViewItem> lv = listView.Items.Cast<ListViewItem>();

var rows =  from x in lv
            where x.SubItems[columnIndex].Text == searchTerm
            select x;

if (rows.Count() > 0)
{
    ListViewItem row = rows.FirstOrDefault();

    //TODO with your row
}