问题描述
我有以下代码,它们在.NET 4.0中再次使用Linq to SQL,再次使用SQL Server 2008 R2服务器:
I have the following code which is using Linq to SQL in .NET 4.0 agains a SQL Server 2008 R2 server:
public void Patients()
{
var documents = GetEMRDocumentsByPatientId("231966");
if (documents.Count() > 0)
{
foreach (var doc in documents)
{
Console.WriteLine(doc.PatientId);
}
}
}
public static IQueryable<EMRDocument> GetEMRDocumentsByPatientId(string inPatientId)
{
return GetEMRDocuments().Where(d => String.Equals(d.PatientId, inPatientId));
}
public static IQueryable<EMRDocument> GetEMRDocuments()
{
var dataContext = InitializeDataContext();
IQueryable<EMRDocument> retVal = null;
retVal = (from e in dataContext.EMREvaluations
select new EMRDocument
{
PatientId = e.PatientId,
IsDeleted = e.Deleted,
}).Union(
from e2 in dataContext.EMRPatientDailyNotes
select new EMRDocument
{
PatientId = e2.PatientID,
IsDeleted = false,
});
return retVal;
}
该应用的启动会调用Patient();我第一次在Patient()中的foreach行上收到指定的转换无效"错误.documents.Count()有效正确并从数据库返回正确的记录数.我还可以采用生成的文档SQL并在SSMS中运行它,结果将正确返回.
The start of the app calls Patients(); I receive a "Specified cast is not valid" error on the foreach line in Patients() the first time in. The documents.Count() works correctly and returns the correct number of records from the database. I can also take the generated SQL of documents and run it in SSMS and the results return correctly.
在运行的查询中,select从dataContext.EMRPatientDailyNotes中不返回任何记录,因为该表中没有231966的PatientId的记录.
In the query that runs, the select, from dataContext.EMRPatientDailyNotes, returns no records, because there is no record in that table for the PatientId of 231966.
在GetEMRDocuments()中,如果我在两个选择中都将IsDeleted = e.Deleted和IsDeleted = false注释掉,则下面的整个代码将正确执行.如果我将IsDeleted = e.Deleted更改为IsDeleted = false,则代码将正常运行.如果我删除整个联盟并开始运行以下...
In GetEMRDocuments(), if I comment out the IsDeleted = e.Deleted and IsDeleted = false in both selects, then the entire code below will execute without error. If I change IsDeleted = e.Deleted to IsDeleted = false, then the code runs without error. If I remove the entire Union and just runthe following...
retVal = (from e in dataContext.EMREvaluations
select new EMRDocument
{
PatientId = e.PatientId,
IsDeleted = e.Deleted,
});
...然后此代码将正确执行.另外,e.Deleted是数据库中的布尔值,而不是布尔值?(可空布尔值),IsDeleted是布尔值.以前有没有人看过这种类型的问题?我不知道该怎么办才能解决此问题.此外,我以前在同一查询中使用Linq To Entities,但未收到此错误.任何帮助将不胜感激.
...then this code executes without error. Also, e.Deleted is a bool from the database and not a bool?(nullable bool) and IsDeleted is a bool. Has anyone seen this type of issue before? I have no idea what to do to fix this. Also, I was previously using Linq To Entities with this same query and did not receive this error. Any help would be greatly appreciated.
谢谢,丹
这是堆栈跟踪.我还有一些其他代码似乎隐藏了实际的错误:
Here's the stack trace. I had some other code that seemed to be hiding the actual error:
[InvalidCastException: Specified cast is not valid.]
System.Data.SqlClient.SqlBuffer.get_Boolean() +5057281
System.Data.SqlClient.SqlDataReader.GetBoolean(Int32 i) +38
Read_EMRDocument(ObjectMaterializer`1 ) +313
System.Data.Linq.SqlClient.ObjectReader`2.MoveNext() +32
ATI.TherapyConnect.Service.Patients() in C:\SVN\Application\branches\dan.cagney\ATI.TherapyConnect\Service.asmx.cs:57
ATI.TherapyConnect.Content.UserControls.Shared.Patients.MyPatientsList.BindGrid(String inSortExpression, Int32 inCurrentPage) in C:\SVN\Application\branches\dan.cagney\ATI.TherapyConnect\Content\UserControls\Shared\Patients\MyPatientsList.ascx.cs:129
ATI.TherapyConnect.Content.UserControls.Shared.Patients.MyPatientsList.Page_Load(Object sender, EventArgs e) in C:\SVN\Application\branches\dan.cagney\ATI.TherapyConnect\Content\UserControls\Shared\Patients\MyPatientsList.ascx.cs:68
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Control.LoadRecursive() +146
System.Web.UI.Control.LoadRecursive() +146
System.Web.UI.Control.LoadRecursive() +146
System.Web.UI.Control.LoadRecursive() +146
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
因此,显然在IsDeleted的布尔值中存在强制转换问题.难道是因为没有从dataContext.EMRPatientDailyNotes返回任何结果,所以它试图将null值映射到Union的IsDeleted中?我不认为这会成为问题,因为如果我只是将IsDeleted = e.Deleted更改为IsDeleted = false,就不会有错误.因此,似乎e.Deleted返回的是非布尔值.但是,如果将e.Deleted定义为Linq To SQL生成的代码中的布尔值,并且SQL数据库中的位字段不是null,那怎么可能呢?
So, apparently there's a casting issue into the bool value of IsDeleted. Could it be that it's trying to map a null value into the IsDeleted of the Union since there are no results returned from dataContext.EMRPatientDailyNotes? I don't think this would be the issue since if I just change IsDeleted = e.Deleted to IsDeleted = false, there is no error. So, it seems that e.Deleted is returning a non-bool value. But how would that be possible when e.Deleted is defined as a bool from the Linq To SQL generated code and it is a bit, not null field in the SQL database?
推荐答案
我终于能够使它生效,但是我不知道为什么需要更改.这是我将查询更改为:
I was finally able to get this to work, however I don't know why my change was required. Here is what I changed my query to:
retVal = (from e in dataContext.EMREvaluations
select new EMRDocument
{
PatientId = e.PatientId,
IsDeleted = e.Deleted == null ? false : e.Deleted,
}).Union(
from e2 in dataContext.EMRPatientDailyNotes
select new EMRDocument
{
PatientId = e2.PatientID,
IsDeleted = false,
});
我修改了
IsDeleted = e.Deleted
到
IsDeleted = e.Deleted == null ? false : e.Deleted
但是我不知道为什么需要这样做,因为e.Deleted在数据库中被列为布尔值(不是null),那么e.Deleted怎么会具有null值?我现在将其标记为答案,但是,如果有人可以告诉我为什么需要此代码更改,或者如果不更改此代码我该如何获得,则将其标记为答案.
however I have no idea why this is needed because e.Deleted is listed as a bool (not null) in the database, so how could e.Deleted ever have a value of null? I am marking this as the answer for now, however if someone can tell me why this code change was needed or how I can get by without this code change, I will mark that as the answer.
谢谢,丹
这篇关于Linq To SQL指定的强制转换与Union无效错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!