Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot implicitly convert type System.Linq.IQueryable<string> to string

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-

Cannot implicitly convert type System.Linq.IQueryable to string

where am I going wrong?

like image 464
prers Avatar asked Dec 11 '22 10:12

prers


1 Answers

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;
 }
like image 170
alexmac Avatar answered Jan 04 '23 19:01

alexmac