Linq IQueryable to Listview

L

Lloyd Sheen

Dean Slindee said:
Has anyone sourced a WinForm listview control from a LINQ query yet?

In my legacy code, the datasource for the listview has always been an
ADO.NET DataTable. The DataTable works well because it provides the
column header names associated with the listview's datacolumn headers.

Can the column header information be provided by IQueryable object?
Anyone have some rudimentary sample code showing how to do this?

Thanks,
Dean Slindee

I am assuming that you are talking ad-hoc queries.

It can be done.

First we have a query as such:

Dim glist = From al In linqItems.SongInfos _
Where al.FolderName.StartsWith(cfi.folderName) _
Select al.Genre, al.Artist Distinct _
Order By Genre

This give you the query variable glist.

Dim typ As Type = glist.ElementType
Dim props() As PropertyInfo = typ.GetProperties

Now if you use the above 2 lines you will see that you have two props. Each
one gives in the Name property of the props variable the name of the the
variables in the query.


I have also used the listview in "virtual" mode to speed things up
considerably. To do this you do the following:

Me.SongFileView.VirtualListSize = songList.Count
Me.SongFileView.VirtualMode = True

This will take a list(of ...) and use it as the "datasource". You will then
handle the RetrieveVirtualItem event of the listview.

Private Sub SongFileView_RetrieveVirtualItem(ByVal sender As Object,
ByVal e As System.Windows.Forms.RetrieveVirtualItemEventArgs) Handles
SongFileView.RetrieveVirtualItem
Dim li As New ListViewItem
Dim si As SongInfo = CType(displayList(e.ItemIndex), SongInfo)
li.Text = si.FileName
li.SubItems.Add(si.Artist)
li.SubItems.Add(si.Title)
li.SubItems.Add(si.Album)
li.SubItems.Add(si.TrackNumber.ToString)
li.SubItems.Add(si.Genre)

e.Item = li
End Sub

What you want to do is from the "datasource" map an attribute to a column.
Haven't done this yet but a query of 33K items comes back with display in a
non-perceptable time frame.

Hope this gets you on your way,
Lloyd Sheen
 
L

Lloyd Sheen

Lloyd Sheen said:
I am assuming that you are talking ad-hoc queries.

It can be done.

First we have a query as such:

Dim glist = From al In linqItems.SongInfos _
Where al.FolderName.StartsWith(cfi.folderName) _
Select al.Genre, al.Artist Distinct _
Order By Genre

This give you the query variable glist.

Dim typ As Type = glist.ElementType
Dim props() As PropertyInfo = typ.GetProperties

Now if you use the above 2 lines you will see that you have two props.
Each one gives in the Name property of the props variable the name of the
the variables in the query.


I have also used the listview in "virtual" mode to speed things up
considerably. To do this you do the following:

Me.SongFileView.VirtualListSize = songList.Count
Me.SongFileView.VirtualMode = True

This will take a list(of ...) and use it as the "datasource". You will
then handle the RetrieveVirtualItem event of the listview.

Private Sub SongFileView_RetrieveVirtualItem(ByVal sender As Object,
ByVal e As System.Windows.Forms.RetrieveVirtualItemEventArgs) Handles
SongFileView.RetrieveVirtualItem
Dim li As New ListViewItem
Dim si As SongInfo = CType(displayList(e.ItemIndex), SongInfo)
li.Text = si.FileName
li.SubItems.Add(si.Artist)
li.SubItems.Add(si.Title)
li.SubItems.Add(si.Album)
li.SubItems.Add(si.TrackNumber.ToString)
li.SubItems.Add(si.Genre)

e.Item = li
End Sub

What you want to do is from the "datasource" map an attribute to a column.
Haven't done this yet but a query of 33K items comes back with display in
a non-perceptable time frame.

Hope this gets you on your way,
Lloyd Sheen

By the way the code to get values from an object where you know the name of
the property is:


Dim typ As Type = si.GetType
Dim props As PropertyInfo = typ.GetProperty("Artist")
Dim val As String = props.GetValue(si, Nothing).ToString
 
D

Dean Slindee

Has anyone sourced a WinForm listview control from a LINQ query yet?

In my legacy code, the datasource for the listview has always been an
ADO.NET DataTable. The DataTable works well because it provides the column
header names associated with the listview's datacolumn headers.

Can the column header information be provided by IQueryable object? Anyone
have some rudimentary sample code showing how to do this?

Thanks,
Dean Slindee
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top