Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 432503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:04:52+00:00 2026-05-12T20:04:52+00:00

I have written a SQL query that works just fine, but am having a

  • 0

I have written a SQL query that works just fine, but am having a little trouble with the conversion to LINQ. Here is the SQL:

SELECT        
  CourseID, 
  CourseName, 
  CreditHours, 
  CPTRequired, 
  COTRequired, 
  CPTElective, 
  COTElective
FROM            
  Courses
WHERE        
  (CPTRequired = 'true') 
  AND 
  (CourseID NOT IN
    (SELECT        
      Courses_1.CourseID
    FROM
      Courses AS Courses_1 INNER JOIN
      Sections ON Sections.CourseID = Courses_1.CourseID INNER JOIN
      Enrollment ON Enrollment.SectionID = Sections.SectionID INNER JOIN
      Students ON Students.StudentID = Enrollment.StudentID
    WHERE        
      (Students.StudentID = '11110004')))

And here is what I have written so far with LINQ:

Dim maj = (From c In connect.Courses _
           Where c.CPTRequired = "True" _
           Select c.CourseID, c.CourseName, c.CreditHours).Except _
          (From en In connect.Enrollments _
           Join s In connect.Sections On en.SectionID Equals s.SectionID _
           Join cs In connect.Courses On s.CourseID Equals cs.CourseID _
           Join st In connect.Students On en.StudentID Equals st.StudentID _
           Order By cs.CourseName _
           Where st.StudentID = StudentID _
           Select cs.CourseID)

When executed the LINQ statement throws the following errors.

System.InvalidCastException was
unhandled Message=”Unable to cast
object of type
‘System.Data.Linq.DataQuery’1[System.String]’
to type
‘System.Collections.Generic.IEnumerable’1
[VB$AnonymousType_6’3[System.String,System.String,System.Nullable`1[System.Int32]]]’.”
Source=”Final Project”

What am I missing? I am brand new to LINQ, so please be gentle!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:04:52+00:00Added an answer on May 12, 2026 at 8:04 pm

    I can’t say for sure because the VB syntax has thrown me off, but I believe it is because in the statement prior to the except you are selecting a new anonymous object (with multiple properties) and in the query being passed to Except you are only selecting a single column. So it is unable to compare the types properly.

    Effectively it appears that you are trying to compare:

    c.CourseID, c.CourseName, c.CreditHours
    

    with

    c.CourseID
    

    and LINQ doesn’t know how to do that properly.. It may work if you expand the secondary select it include c.CourseName and c.CreditHours

    Making it:

    Dim maj = (From c In connect.Courses _
               Where c.CPTRequired = "True" _
               Select c.CourseID, c.CourseName, c.CreditHours).Except _
              (From en In connect.Enrollments _
               Join s In connect.Sections On en.SectionID Equals s.SectionID _
               Join cs In connect.Courses On s.CourseID Equals cs.CourseID _
               Join st In connect.Students On en.StudentID Equals st.StudentID _
               Order By cs.CourseName _
               Where st.StudentID = StudentID _
               Select cs.CourseID, cs.CourseName, cs.CreditHours)
    

    But again i’m not all that familiar with the VB syntax so best I can say is give it a shot.

    Edit: Moving Context logging code from comment to answer..

    Dim sb As New StringBuilder
    Dim sw As New StringWriter(sb)
    connect.Log = sw
    ...Do Query & Enumerate Results...
    sb.ToString() 'will contain the generated SQL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this linq query that works well (although it may be written better,
I have a SQL query which is written in string and then executed using
Hai Techies, I have some stored procedure which was written in SQL server.Now i
Ok, I have an application written in C#. We have data in an SQL
I have written an AIR Application that downloads videos and documents from a server.
I have written a site in Prototype but want to switch to jQuery. Any
I have written a DLL that uses MS Word to spell check the content
I have written something that uses the following includes: #include <math.h> #include <time.h> #include
I have a view defined in SQL server 2008 that joins 4 tables together.
I have a query which has an Order By clause. The generated SQL from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.