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?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With