Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Getting Columns from LINQ List
Message
From
03/07/2011 16:35:40
 
 
To
03/07/2011 15:19:20
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01517212
Message ID:
01517215
Views:
32
Don't know for sure, Gunnar, but this might work:

Since you've got this code:
        Dim result_0 =
                From col_Row In db.Masters, z In db.DocumentTypes
                Select
                    ListCol =
                        VendorID = col_Row.VendorID,
                        Vendor_Name = col_Row.Vendor_Name,
...
        Dim TheList = result_0.ToList()

        ' the following line filters results based on user permissions
        Dim result_Match = (From x In TheList Where x.SecurityCode.IncludesAnyOf(UserPermissions) Select x).ToList()
Then your result_Match should actually be a List (of ListCol), I think. If so, you should be able to maybe do something like this:
         Dim NewDT As DataTable = ToDataTable(Me.MyGridView1.DataSource As List (of ListCol))
Assuming I'm even close with the VB syntax (sorry, VB is not my strong point).

~~Bonnie



>Hello Everybody.
>
>(I am using VB.net in VS2010)
>
>I got stuck trying to convert a list created with LINQ for a gridview to a datatable.
>
>Here is what i need to do:
>
>I've created a filtered list with LINQ to populate a gridview. At a later point in time the user should be able to export the data from the filtered list. I can not go through the gridview because in I am not showing all the fields collected. So ineed to loop through the DataSource from the gridview.
>
>since i want to allow the user to pick which fields they want to export this has to be dynamic. I have not found a way to get a column count or collection from the list. So i thought i convert the list to a datatable. I found a routine on the web but i am getting the error "Type T is not defined" which obviously is the "iList (of T)".
>
>I know i am missing something obvious.
>
>Here is what I have:
>(this populates the gridview i left a lot code out to simplify)
>        Dim db = New DataClassesDataContext()
>        Dim result_0 =
>                From col_Row In db.Masters, z In db.DocumentTypes
>                Select
>                    ListCol =
>                        VendorID = col_Row.VendorID,
>                        Vendor_Name = col_Row.Vendor_Name,
>...
>        If String.IsNullOrWhiteSpace(Me.Project.Text) = False And Me.Project.Text <> "0000" Then
>            result_0 = result_0.Where(Function(f) f.Project_ID.ToLower = Project.Text.ToLower)
>        End If
>...
>        Dim TheList = result_0.ToList()
>
>        ' the following line filters results based on user permissions
>        Dim result_Match = (From x In TheList Where x.SecurityCode.IncludesAnyOf(UserPermissions) Select x).ToList()
>
>        ' populate the gui items
>        If result_Match.Count() > 0 Then
>            MyGridView1.DataSourceID = ""
>            MyGridView1.DataSource = result_Match
>            'MyGridView1.DataKeyNames = "Doc_ID,DocLocation"
>            MyGridView1.DataBind()
>            MyGridView1.SelectedIndex = 0
>            Dim args As New GridViewRowClickedEventArgs(MyGridView1.Rows(0))
>            Me.Doc_Count.Text = "Your Search has returned " & result_Match.Count().ToString & " documents."
>            GridView1_RowClicked(MyGridView1, args)
>        endif
>
>The following is where I am trying to collect the data
>...   
>         Dim NewDT As DataTable = ToDataTable(Me.MyGridView1.DataSource)
>            ' 
>            Dim i As Integer = 0
>            Do While (i < NewDT.Rows.Count)
>                ' build exported data file
>                For Each li In NewDT.Rows(i).ItemArray
>                    DataLine.Add(NewDT.Rows(i).Item(li.value).ToString.Trim)
>                Next
>                strData += DataLine.ToString + vbCrLf
>                '
>                i = (i + 1)
>            Loop
>
>...
>
>    Public Shared Function ToDataTable(ByVal data As IList(Of T)) As DataTable
>        Dim props As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(T))
>        Dim table As DataTable = New DataTable
>        Dim n As Integer = 0
>        Do While (n < props.Count)
>            Dim prop As PropertyDescriptor = props(n)
>            table.Columns.Add(prop.Name, prop.PropertyType)
>            n = (n + 1)
>        Loop
>        Dim values() As Object = New Object((props.Count) - 1) {}
>        For Each item As T In data
>            Dim x As Integer = 0
>            Do While (x < values.Length)
>                values(x) = props(x).GetValue(item)
>                x = (x + 1)
>            Loop
>            table.Rows.Add(values)
>        Next
>        Return table
>    End Function
>
>
>Thank you for all your help!
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform