繁体   English   中英

如何在linq查询中强制将字符串转换为int

[英]How to force a conversion of string to int in a linq query

我收到错误

LINQ to Entities无法识别方法'Int32 Parse(System.String)'方法,并且该方法无法转换为商店表达式

在我的问题被重复之前,请完整阅读。 我四处搜寻并查看了一些解决方案,但似乎没有一个适合这个方案。

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

错误在转换中落在我的where子句中

其中Convert.ToInt32(instruct.InstructorInstrNo)== royalHIstory.RoyalIns

我需要那种转变

Convert.ToInt32(instruct.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()
});

暂无
暂无

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

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