Cannot implicitly convert type System.Linq.IQueryable to string

c# entity-framework iqueryable linq

Question

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?

1
4
11/9/2013 11:37:04 AM

Popular Answer

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;
 }
13
11/9/2013 11:38:33 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow