LINQ 与 BIT 列的联合导致指定的强制转换无效错误

发布于 2024-10-21 07:45:19 字数 2231 浏览 0 评论 0原文

我在几个表之间有一个复杂的连接,但我已经成功使用 linqpad 和下面的小表复制了错误。 COLNAME 列和 YAXIS 列之间以及 COLNAME 和 XAXIS 之间存在未显式定义的引用。

错误是“指定的转换无效”,最初我浪费时间认为问题是在 VS 2010 中转换返回到我的对象的数据,但错误也发生在没有定义对象的 linqpad 中。一个位列会导致这个问题,这似乎很疯狂。如果我将列类型更改为 VARCHAR,它就可以正常工作。如果我从 linqpad 或 sql profiler 运行生成的 SQL,也能正常返回。

到底是怎么回事?

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)

这是 linq 语句:

((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()

编辑: 我已向 microsoft 提交了错误 此处

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终遇你 2024-10-28 07:45:19

生成的 sql 包含以下行,其中 @p4 对应于投影中的 Blahblahblah=false 行:

DECLARE @p4 Int = 0

并且从查询返回的 int 无法转换为 bool。我不知道这是否是 linq to sql 错误(看起来像),但有一个解决方法。基本上,您需要从投影的匿名类型中删除 Blahblahblah=false ,然后删除 .ToList().ToArray() 结果,然后最后在 linq to objects 投影中添加 bool 字段:

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();

请注意,这会导致两个 sql 查询,而不是一个。

编辑

另外,可以省略 Distinct(),因为 union 不包含重复项。我实际上应该阅读我复制和粘贴的代码!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文