Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq in selecting item from ListViewItemCollections in c#

Tags:

c#

linq

how to select a ListViewItem from ListViewItemCollections using Linq in C#?

i tried on using this but it didn't work..

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..
like image 332
Vincent Dagpin Avatar asked Jul 10 '11 09:07

Vincent Dagpin


People also ask

How do you use take and skip in Linq?

The Take operator is used to return a given number of rows from a database table and the Skip operator skips over a specifed number of rows in a database table. I create a data context class that has tables or a stored procedure.

What does Linq Select Return?

By default, LINQ queries return a list of objects as an anonymous type. You can also specify that a query return a list of a specific type by using the Select clause.

What is select new Linq?

@CYB: select new is used when you want your query to create new instances of a certain class, instead of simply taking source items. It allows you to create instances of a completely different class, or even an anonymous class like in OP's case.


1 Answers

To get an enumerator of ListViewItem, you have to cast the Items collection of ListView:

IEnumerable<ListViewItem> lv = listview1.items.Cast<ListViewItem>();

Then, you can use LINQ with it:

 var test = from xxx in lv 
            where xxx.text = "data 1" 
            select xxx;
like image 118
Homam Avatar answered Sep 21 '22 15:09

Homam