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 4053760
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:29:17+00:00 2026-05-20T14:29:17+00:00

I have a complicated join between a few tables but I have managed to

  • 0

I have a complicated join between a few tables but I have managed to replicate the error using linqpad and the small tables below. There are references between the COLNAME column and the YAXIS column and also between COLNAME and XAXIS that is not explicitly defined.

The error is “Specified cast is not valid”, which originally I wasted time thinking the problem was converting the data returned to my object in VS 2010, but the error also happens in linqpad with no defined object. It seems insane that a bit column would cause this problem. If I change the column type to a VARCHAR it works fine. If I run the generated SQL from linqpad or sql profiler that also returns fine.

What is going on?

CREATE TABLE TEST_QUERY 
([ID] INT IDENTITY(1,1) PRIMARY KEY,
 blah VARCHAR(20))

CREATE TABLE TEST_QUERY_COLS
(QUERYID INT NOT NULL,
 COLNAME VARCHAR(20) NOT NULL,
 otherblah VARCHAR(20),
 PRIMARY KEY(QUERYID,COLNAME))

CREATE TABLE TEST_CHART
(CHARTID INT IDENTITY(1,1) PRIMARY KEY,
 QUERYID INT NOT NULL REFERENCES TEST_QUERY([ID]),
 XAXIS VARCHAR(20) NOT NULL,
 blahblah VARCHAR(20))

CREATE TABLE TEST_CHART_SERIES
(CHARTID INT NOT NULL REFERENCES TEST_CHART(CHARTID),
 YAXIS VARCHAR(20) NOT NULL,
 blahblahblah BIT NOT NULL,
 PRIMARY KEY(CHARTID,YAXIS))

INSERT INTO TEST_QUERY(blah) VALUES('xxx')
INSERT INTO TEST_QUERY_COLS(QUERYID,COLNAME,otherblah) VALUES(1,'col1','xxx')
INSERT INTO TEST_QUERY_COLS(QUERYID,COLNAME,otherblah) VALUES(1,'col2','yyy')
INSERT INTO TEST_CHART(QUERYID,XAXIS,blahblah) VALUES(1,'col1','xxx')
INSERT INTO TEST_CHART_SERIES(CHARTID,YAXIS,blahblahblah) VALUES(1,'col2',1)

This is the linq statement:

((from ch in TEST_CHARTs
join a in TEST_CHART_SERIES on ch.CHARTID equals a.CHARTID into a_join
from cs in a_join.DefaultIfEmpty()
join ycols in TEST_QUERY_COLS on new { key1 = cs.YAXIS, key2 = ch.QUERYID } equals new { key1 = ycols.COLNAME, key2 = ycols.QUERYID }
where ch.CHARTID == 1
select new 
{
    ch.CHARTID,
    POSITION = 0,
    ycols.QUERYID,
    ycols.Otherblah,
    cs.Blahblahblah
})
.Union(from ch in TEST_CHARTs
join xcol in TEST_QUERY_COLS on new { key1 = ch.XAXIS, key2 = ch.QUERYID } equals new { key1 = xcol.COLNAME, key2 = xcol.QUERYID }
where ch.CHARTID == 1
select new 
{
    ch.CHARTID,
    POSITION = 0,
    xcol.QUERYID,
    xcol.Otherblah,
    Blahblahblah = false
})).Distinct()

Edit: I’ve filed a bug with microsoft here

  • 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-20T14:29:18+00:00Added an answer on May 20, 2026 at 2:29 pm

    The generated sql includes the following line, where @p4 corresponds to the Blahblahblah=false line in your projection:

    DECLARE @p4 Int = 0
    

    And the int that is returned from the query can’t be converted to a bool. I don’t know whether or not this is a linq to sql bug (seems like it), but there is a workaround. Basically you need to drop the Blahblahblah=false from the anonymous type projected, then .ToList() or .ToArray() the result, and finally add the bool field in a linq to objects projection:

    var one =
        (from ch in TEST_CHARTs
        join a in TEST_CHART_SERIES on ch.CHARTID equals a.CHARTID into a_join
        from cs in a_join.DefaultIfEmpty()
        join ycols in TEST_QUERY_COLS on new { key1 = cs.YAXIS, key2 = ch.QUERYID } equals new { key1 = ycols.COLNAME, key2 = ycols.QUERYID }
        where ch.CHARTID == 1
        select new 
        {
            ch.CHARTID,
            POSITION = 0,
            ycols.QUERYID,
            ycols.Otherblah,
            Blahblahblah = cs.Blahblahblah
        }).ToList();
    
    var two =
        (from ch in TEST_CHARTs
        join xcol in TEST_QUERY_COLS on new { key1 = ch.XAXIS, key2 = ch.QUERYID } equals new { key1 = xcol.COLNAME, key2 = xcol.QUERYID }
        where ch.CHARTID == 1
        select new 
        {
            ch.CHARTID,
            POSITION = 0,
            xcol.QUERYID,
            xcol.Otherblah
        }).ToList();
    
    var three = 
        from x in two
        select new
        {
            x.CHARTID,
            x.POSITION,
            x.QUERYID,
            x.Otherblah,
            Blahblahblah = false
        };
    
    var four = one.Union(three).Distinct();
    

    Note that this results in two sql queries, not one.

    EDIT

    Also, Distinct() can be left out, since union doesn’t include duplicates. I should have actually read the code that I copied and pasted!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a fairly complicated GUI in windows forms using C# and .Net 2.0.
I have a query that requires what I think is a complicated JOIN. I
I'm very novice with Linq but I have to convert several complicated SQL queries
I have a Linq-to-Entities query that is not complicated but requires an .include and/or
I have been thinking about a complicated view/query I need to create but I
I have a fairly complicated join query that I use with my database. Upon
I have a complicated problem and I hope to explain it clearly possible... I
I have a complicated MySQL query which takes a lot of time, selecting from
I have a complicated report that I need to draw with GDI+ (I don't
I've got a program where a lot of classes have really complicated configuration requirements.

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.