C# 如何在linq查询中强制将字符串转换为int
我发现了错误 LINQ to Entities无法识别方法“Int32 Parse(System.String)”方法,并且无法将此方法转换为存储表达式 在我的问题被称为重复之前,请仔细阅读。我四处寻找并寻找了一些解决方案,但似乎没有一个适合这一点C# 如何在linq查询中强制将字符串转换为int,c#,entity-framework,linq,C#,Entity Framework,Linq,我发现了错误 LINQ to Entities无法识别方法“Int32 Parse(System.String)”方法,并且无法将此方法转换为存储表达式 在我的问题被称为重复之前,请仔细阅读。我四处寻找并寻找了一些解决方案,但似乎没有一个适合这一点 var query = from royalHIstory in ctx.tblRoyalHistories join historyComment in ctx.tblRoyalHistoryComments on royalHIs
var query = from royalHIstory in ctx.tblRoyalHistories
join historyComment in ctx.tblRoyalHistoryComments
on royalHIstory.RoyalHistoryID equals historyComment.RoyalHistoryID
orderby royalHIstory.IndexNum ascending
where royalHIstory.InstNmbr == instnmbr
select new
{
RoyalHistoryID = royalHIstory.RoyalHistoryID,
RoyalHistoryCommentID = historyComment.RoyalHistoryCommentID,
InstNmbr = royalHIstory.InstNmbr,
IndexNum = royalHIstory.IndexNum,
RoyalIns = royalHIstory.RoyalIns,
RoyalComment = historyComment.Comment,
Name = (from memberName in ctx.tblMembers
join instruct in ctx.tblInstructors
on memberName.MemberID equals instruct.MemberID
where Convert.ToInt32(instruct.InstructorInstrNo) == royalHIstory.RoyalIns
select memberName.MemberFirstName + " " + memberName.MemberLastName).FirstOrDefault()
};
错误落在转换中的my where子句上
其中Convert.ToInt32(instruct.InstructorInstrNo)=royalHIstory.RoyalIns
我需要这种转变
将.ToInt32转换为(指令.InstructorInstrNo)
我不知道如何强迫它发生你不能使用
Convert.ToInt32(instruct.InstructorInstrNo)
在LINQ查询中,如果要转换为int,请尝试以下操作
var query = (from royalHIstory in ctx.tblRoyalHistories
join historyComment in ctx.tblRoyalHistoryComments
on royalHIstory.RoyalHistoryID equals historyComment.RoyalHistoryID
orderby royalHIstory.IndexNum ascending
where royalHIstory.InstNmbr == instnmbr
select new {royalHIstory,historyComment }).AsEnumerable().Select(row=>new
{
RoyalHistoryID = row.royalHIstory.RoyalHistoryID,
RoyalHistoryCommentID = row.historyComment.RoyalHistoryCommentID,
InstNmbr = row.royalHIstory.InstNmbr,
IndexNum = row.royalHIstory.IndexNum,
RoyalIns = row.royalHIstory.RoyalIns,
RoyalComment = row.historyComment.Comment,
Name = (from memberName in ctx.tblMembers
join instruct in ctx.tblInstructors
on memberName.MemberID equals instruct.MemberID
where Convert.ToInt32(instruct.InstructorInstrNo) == row.royalHIstory.RoyalIns
select memberName.MemberFirstName + " " + memberName.MemberLastName).FirstOrDefault()
});
在LINQ中,以实体-不可能。尝试相反的
instructor.InstructorInstrNo==royalHIstory.RoyalIns.ToString()
,这是受支持的。我希望您仍然可以调整数据模型。如果两个字段包含相似的数据(当您比较它们时,它们会这样做!),那么它们的数据类型应该不同是毫无意义的。如果数据模型是固定的,那么试着踢谁做的。为什么你认为这不是重复的?问题仍然是一样的,您使用的NET Framework方法无法转换为某种Sql(在本例中为Convert.ToInt32)@IvanStoev,我之前尝试过,但查询缺少一条记录,当我检查此查询生成的Sql时,它将royalHIstory.RoyalIns转换为varchar,这是预期的,并且它总是基于id(仅3个字符长)错过第一条记录。因此,在SQL脚本中,如果我将instruction.instructionsrno强制转换为int,并将强制转换作为varchar保留在royalHIstory.RoyalIns上,那么我将在不丢失MemberFirst和LastName的情况下获得正确的记录,这很简单。L2E不支持从字符串转换-没有CLR方法,没有规范函数。没有解决方案。select new语句中的变量不存在获取。不清楚…请详细解释。但这是真的,结果会是你想要的。