none
Error creating a DataView using LINQ RRS feed

  • Question

  • Below is my code, along with the runtime error it produces. Does anyone have some suggestions for eliminating the error?


    -------------------

             Dim cht1tbl = MetDs.Tables("Charts").AsEnumerable

            Dim query = _
                        From d In cht1tbl.AsEnumerable _
                        Select d!Jan_2006, d!Feb_2006

            Dim cht1View As New DataView
            cht1View = query.AsQueryable


    -------------------

    System.InvalidCastException was unhandled by user code
      Message="Unable to cast object of type 'System.Linq.EnumerableQuery`1[VB$AnonymousType_0`2[System.Object,System.Object]]' to type 'System.Data.DataView'."
      Source="App_Web_fuzhh4da"
      StackTrace:
           at _Default.LoadGrdData() in C:\Users\Paul\Documents\Visual Studio 2008\WebSites\TestGrid\Default.aspx.vb:line 33
           at _Default.Page_Load(Object sender, EventArgs e) in C:\Users\Paul\Documents\Visual Studio 2008\WebSites\TestGrid\Default.aspx.vb:line 17
           at System.Web.UI.Control.OnLoad(EventArgs e)
           at System.Web.UI.Control.LoadRecursive()
           at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
      InnerException:

    Thursday, January 22, 2009 10:52 PM

Answers

All replies

  • Linq to DataSet doesn't support creating DataView from a projection (aka an anonymous class).

    Try just creating a dataview.
        Dim cht1View as New DataView(MetDs.Tables("Charts"))

    or

            Dim cht1tbl = MetDs.Tables("Charts").AsEnumerable
            Dim query = _
                        From d In cht1tbl.AsEnumerable _
                        Select d

            Dim cht1View As DataView
            cht1View = query.AsDataView()

    If you are creating a DataView for binding to a grid and only want those 2 columns displayed, you have to modifiy the grid to make that happen, otherwise by default all the columns in the table are shown.


    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to mark the replies as answers if they help.
    Saturday, January 24, 2009 2:23 AM
    Moderator
  •  

    I think I may have found the answer to my own question, here:

    http://msdn.microsoft.com/en-us/library/bb669096.aspx

     

    • Marked as answer by StudentPaul Saturday, January 24, 2009 1:35 PM
    Saturday, January 24, 2009 1:35 PM