locked
Error with LINQ to DataTable RRS feed

  • Question

  • User-805433916 posted

    Why does the following code give an error?

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

    Dim cht1tbl As DataTable = MetDs.Tables("Charts")

    Dim query = _
    From d In cht1tbl.AsEnumerable _
    Select _
                d.Field(
    Of Decimal)("Jan_2006"), _
                d.Field(
    Of Decimal)("Feb_2006")

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

    If I change the code to remove the second field, as below, then the code does not give an error.

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

    Dim cht1tbl As DataTable = MetDs.Tables("Charts")

    Dim query = _
    From d In cht1tbl.AsEnumerable _
    Select _
                d.Field(
    Of Decimal)("Jan_2006")

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

    The error message is as follows:

    "Range variable name can be inferred only from a simple or qualified name with no arguments"

    Friday, January 23, 2009 1:18 PM

Answers

All replies

  • User-294030300 posted

    Hey,

    I don't know what the syntax is in VB.NET, but in C# you do:

    from d in cht1tbl.AsEnumerable()
    select new
    {
       Jan2006 = d.Field<decimal>("Jan_2006"),
       Feb2006 = d.Field<decimal>("Feb_2006")
    }

     You have to assign a property name for the anonymous type with multiple fields; in the last declaration, it sees an array of decimals, in the other, it will see a custom object.

     

    Friday, January 23, 2009 1:28 PM
  • User-805433916 posted

    Okay, here's my new code, in which the last line (cht1View = query.AsQueryable) produces the error that follows. Any suggestions as to how I can eliminate the error?

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

            Dim cht1tbl = MetDs.Tables("Charts")

            Dim query = _
                        From d In cht1tbl.AsEnumerable _
                        Select New With {d!PrimKey, 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`3[System.Object,System.Object,System.Object]]' to type 'System.Data.DataView'."
      Source="App_Web_tileeczb"
      StackTrace:
           at _Default.LoadGrdData() in C:\Users\Paul\Documents\Visual Studio 2008\WebSites\TestGrid\Default.aspx.vb:line 39
           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:

     

    Friday, January 23, 2009 2:58 PM
  • User-294030300 posted

    Hey,

    So that's the VB.NET syntax.  Thanks for that.  The issue is that query is a collection of anonymous type objects; anonymous types are represented by an automatic class name defined by its signature (properties).  But you can't convert this to a DataView; it's no longer a DataTable when you do this.  You can say select d, which selects rows, but again that wouldn't be a data view because it's not a DataTable object, but IQueryable<DataRow>.

    What do you need to do, I can help further...

    Friday, January 23, 2009 3:10 PM
  • User-805433916 posted

    I am creating a dashboard ASP.Net project; I have a feeder data table with LOTS of data; I need to select just certain bits of certain tables, and stash these away as a DataSet which I can save to session and then retrieve as necessary. With my in-memory dataset, I need to do fancy SQL-Select queries to create datasources for grids and charts (Infragistics). The users will pick from various available charts, and will use sliders and grids to adjust various figures (e.g., what if I increase January revenues by 5%). I had everything working correctly as long as I was using regular SQL commands via DataAdapter and DataConnection, but since I moved to an in-memory DataSet (per the boss's instructions) I am at a loss as to how to get SQL-style queries to operate on the in-memory DataSet. People have told me to use LINQ to datasets, but the code samples I have found online (and in my "Programming MS LINQ" book) do not work--I keep getting errors, even though I am just copying the code verbatim--or trying to conver the C examples into VB. I am very new to VB2008 (was programming in Linux for many years previously)

    Friday, January 23, 2009 3:55 PM
  • User-805433916 posted

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

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

    Thanks for the help. God bless.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2009 5:42 PM