无法隐式转换类型System.Linq.IQueryable<串GT;字符串 [英] Cannot implicitly convert type System.Linq.IQueryable<string> to string

查看:517
本文介绍了无法隐式转换类型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;
 }

这篇关于无法隐式转换类型System.Linq.IQueryable<串GT;字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆