有关的SQLException UNION,INTERSECT和EXCEPT [英] SqlException about UNION, INTERSECT and EXCEPT

查看:4254
本文介绍了有关的SQLException UNION,INTERSECT和EXCEPT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能帮助我与此异常?我不明白这意味着什么或如何解决它...它与下面的消息一个SQLException:

Could someone help me with this exception? I don't understand what it means or how to fix it... It is an SqlException with the following message:

所有查询使用UNION合并,相交或EXCEPT运算符必须在其目标列表中的前pressions相同数目的。

All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.

我在运行伪code查询时看起来像这样得到它:

I get it when running a query in pseudo code looking like this:

// Some filtering of data
var query = data.Subjects
            .Where(has value)
            .Where(has other value among some set of values);

// More filtering, where I need to have two different options
var a = query
            .Where(some foreign key is null);
var b = query
            .Where(some foreign key is not null)
            .Where(and that foreign key has a property which is what I want);
query = a.Union(b);

// Final filter and then get result as a list
var list = query
            .Where(last requirement)
            .ToList();

如果我删除 a.Union(B)部分,它在运行时例外。所以我知道错误是存在的。可是为什么我得到它?我该如何解决?我做事情太疯狂了吗?难道我误解了如何使用联盟的事情吗?

If I remove the a.Union(b) parts, it runs without the exception. So I know the error is there. But why do I get it? And how can I fix it? Am I doing something too crazy here? Have I misunderstood how to use the Union thing?

基本上我有一些实体其中有一个外键到其他实体。我需要得到所有要么有一个外键设置为或实体,其中,外国实体满足一些要求。

Basically what I have is some entities which have a foreign key to some other entity. And I need to get all the entities which either have that foreign key set to null or where that foreign entity fulfills some requirements.

推荐答案

由于这看起来像生成的SQL的一个问题,你应该尝试必须使用一个SQL事件探查器,或使用的这code为DebuggerWritter类写的SQL在Visual Studio中的输出窗口。

Since this looks like a problem with the generated SQL, you should try to use either an SQL Profiler, or use this code for DebuggerWritter class to write the SQL to your Output Window in Visual Studio.

的SQL错误通常由UNION检索的字段造成不是为2的查询的相同。例如,如果第一查询可能具有3场,但第二查询有4个字段,会发生这种误差。所以,看到生成的SQL肯定会在这种情况下提供帮助。

The SQL error is normally caused by the fields retrieved for UNION is not the same for the 2 queries. For example, if the first query might have 3 fields, but the second query has 4 fields, this error will occur. So, seeing the generated SQL will definitely help in this case.

这篇关于有关的SQLException UNION,INTERSECT和EXCEPT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆