无法隐式转换类型System.Linq.IQueryable<串GT;字符串

无法隐式转换类型System.Linq.IQueryable<串GT;字符串

问题描述:

我有一个名为秩两个表和CrewMembers.I想获取秩的名称,它存在于排名表CrewMember.I的ID的基础上已通过crewId作为参数,并,该方法的基础上,上将返回该特定船员Member.This的等级是我的代码是如何 -

I have two tables named Rank and CrewMembers.I want to fetch the name of rank which is present in Rank table on the basis of id of CrewMember.I have passed the crewId as a parameter and on the basis of that the method would return the Rank of that specific Crew Member.This is how my code is-

       public string GetRank(int CrewId)
       {

        string rank = (from r1 in context.Rank
            join r2 in context.CrewMember on r1.RankId equals r2.RankId
            where r2.CrewId == CrewId
            select r1.RankName);

        return rank;
      }

当我建立的代码,我碰到下面的错误 -

When I build the code ,I get the following error-

无法隐式转换类型System.Linq.IQueryable为字符串

Cannot implicitly convert type System.Linq.IQueryable to string

我要去哪里错了。

您应该使用FirstOrDefault或的SingleOrDefault:

You should to use FirstOrDefault or SingleOrDefault:

public string GetRank(int CrewId)
{       
    string rank = (from r1 in context.Rank
        join r2 in context.CrewMember on r1.RankId equals r2.RankId
        where r2.CrewId == CrewId
        select r1.RankName).SingleOrDefault();

    return rank;
 }