简体   繁体   English

C#在LINQ查询和事务范围中检查null,导致底层连接无法打开

[英]C# Check null in LINQ query and Transaction Scope giving underlying connection failed to open

I have been messing with 2 problems for quite some time and i have tried alot of things but still i am getting the problems. 很长一段时间我一直在搞乱2个问题而且我已经尝试了很多东西,但我仍然遇到了问题。 Following is the simplified version of the first problem. 以下是第一个问题的简化版本。

I have a LINQ query in which p.XP and XP is of type double? 我有一个LINQ查询,其中p.XP和XP是double类型? . When the value of XP is "null" the query return wrong results even though i have a null in the p.XP field in the database. 当XP的值为“null”时,即使数据库中的p.XP字段中有空值,查询也会返回错误的结果。

from p in entity.positions
where p.XP == XP
select p;

I also tried different things suggested by other threads on SO like 我也尝试过其他线程建议的不同的东西

from p in entity.positions
where Object.Equals(p.XP , XP)
select p;

This gives me following exception 这给了我以下例外

Unable to cast type System.Nullable to System.Object. 无法将System.Nullable类型强制转换为System.Object。

How to resolve this issue ? 如何解决这个问题?

Problem 2: For LINQ i am using TransactionScope to handle transactions. 问题2:对于LINQ,我使用TransactionScope来处理事务。 When i execute the queries i get an exception 当我执行查询时,我得到一个例外

Underlying connection failed to Open. 底层连接无法打开。

and the inner exception is saying something about DTC. 而内在的例外是关于DTC的说法。 I read online that i will have to enable some service on the server. 我在网上看到,我必须在服务器上启用一些服务。 But i donot want to do that. 但我不想这样做。 How can i use transcactions without enabling DTC. 如何在不启用DTC的情况下使用transcactions。 My code is something like this 我的代码是这样的

public void function1()
{
     using(TransactionScope t = new TransactionScope())
     {
         RunSomeSelectQueries();
         RunSomeInsertQueries();
         RunSomeUpdate Queries();

         t.Complete();
     }
}

In your first problem, you need to check for nulls separately from the comparisons, because nulls are treated separately. 在第一个问题中,您需要与比较分开检查空值,因为空值是分开处理的。

   var foo = (from p in entity.positions
                where (p.XP == null && XP == null)
                  || (p.XP == XP)
              select p);

These are really two problems and should be separated into two questions. 这些实际上是两个问题,应分为两个问题。

But I do have an answer for the first one. 但我确实有第一个答案。 In SQL database NULL is not equal to NULL. 在SQL数据库中,NULL不等于NULL。 This is because NULL is meant to represent and unknown value and it is not possible to declare that an unknown is equal to another unknown. 这是因为NULL意味着表示和未知值,并且不可能声明未知等于另一个未知。

To clarify: 澄清:

"SELECT * FROM sometable where 1 = 1" would return all rows, because "1 = 1" is TRUE “SELECT * FROM sometable where 1 = 1”将返回所有行,因为“1 = 1”为TRUE

"SELECT * FROM sometable where 1 = 0" would return no rows, because "1 = 0" is FALSE “SELECT * FROM sometable where 1 = 0”将不返回任何行,因为“1 = 0”为FALSE

"SELECT * FROM sometable where NULL = NULL" or "SELECT * FROM sometable where 1 = NULL" would return no rows, because "NULL = NULL" or "1 = NULL" is equal to a new "boolean" state called NULL. “SELECT * FROM sometable where NULL = NULL”或“SELECT * FROM sometable where 1 = NULL”将不返回任何行,因为“NULL = NULL”或“1 = NULL”等于称为NULL的新“布尔”状态。 A query will only return a row if the WHERE clause is TRUE. 如果WHERE子句为TRUE,则查询仅返回一行。

To extract the row where XP is NULL, you must expand your where clause to something like this: 要提取XP为NULL的行,必须将where子句扩展为如下所示:

from p in entity.positions
where p.XP == XP || (p.XP == null && XP == null)
select p;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM