Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone tell me why my LINQ query function is failing?
VB
Public Function PC_Archived(ByVal strPC As String, _
                                ByVal dtName As DataTable) _
                                As Long
        'Purpose:       Check as see if this PC is registered  
        '               for archiving
        'Parameters:    strPC - PC's Name we are looking for
        '               dtName - The DataTable where the PC 
        '                        names are stored
        'Returns:
        Dim lngPCID As Long
        Debug.Print("Table: " & dtName.TableName.ToString & vbCrLf & _
                    vbTab & "Numder of Rows: " & dtName.Rows.Count.ToString)
        Dim Query = _
            From PC In dtName.AsEnumerable() _
            Where PC.Field(Of String)("PC_Name") = strPC _
                Select New With _
                    { _
                        .PCID = PC.Field(Of Long)("PCID"), _
                        .PCName = PC.Field(Of String)("PC_Name") _
                    }
        For Each PC In Query
            Debug.Print("PCID: " & PC.PCID)
            Debug.Print("PC Name: " & PC.PCName)
            lngPCID = PC.PCID
        Next
        Return lngPCID
    End Function


When it executes the For Each statement I get "InvalidCastException was Unhandled." error.

Thanks,
Posted

Figured it out.

VB
Dim Query = (From PC In dtName _
             Where PC.Field(Of String)("PC_Name") = strPC _
             Select PC.Field(Of Int32)("PCID")).FirstOrDefault()
 
Share this answer
 
This query returns an anonymous object -
SQL
Dim Query = _
          From PC In dtName.AsEnumerable() _
          Where PC.Field(Of String)("PC_Name") = strPC _
              Select New With _
                  { _
                      .PCID = PC.Field(Of Long)("PCID"), _
                      .PCName = PC.Field(Of String)("PC_Name") _
                  }


Thus you cannot directly convert it to PC in For Each PC In Query
VB
For Each PC In Query
      Debug.Print("PCID: " & PC.PCID)
      Debug.Print("PC Name: " & PC.PCName)
      lngPCID = PC.PCID
  Next
 
Share this answer
 
Comments
Quecumber256 21-Jan-14 14:42pm    
Thank you.
Can you give me a hint on how to use a similar query where it takes the strPC variable and if it finds a match returns the PCID number?

This worked once when I had a prefilled data table. I cleared the DB table and entered in just one record. Then it started giving me the aforementioned error.

All data is stored in a data set that is loaded from an Access database.
Ron Beyer 21-Jan-14 15:26pm    
You could add .ToList at the end of your query, or in your for-each like:

For Each PC in Query.ToList

Quecumber256 21-Jan-14 15:33pm    
No cigar. Still get the same error.
Geez! how hard is it to write a simple query to return a value from a DataTable?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900