C# 使用Linq返回groupId

C# 使用Linq返回groupId,c#,linq,C#,Linq,我有这门课: public class Person { public int Id { get; set; } public string UserName { get; set; } public int GroupId { get; set; } } 如果我已经给出了Id,如果我想返回GroupId,该怎么办。。像 public Person GetGroupId (int id) { return _context.Person.Where(c =

我有这门课:

public class Person
{
   public int Id { get; set; }
   public string UserName { get; set; }
   public int GroupId { get; set; }     
}
如果我已经给出了
Id
,如果我想返回
GroupId
,该怎么办。。像

public Person GetGroupId (int id)
{
    return  _context.Person.Where(c => c.Id == id)
                           .Select(x =>  x.GroupId)
                           .FirstOrDefault();
}

但这会给我错误消息,如“无法隐式转换类型”。

查询返回的类型与方法定义的返回类型不匹配

更新方法以返回所需类型,在本例中为
int

public int GetGroupId (int id) {
    return  _context.Person.Where(c => c.Id == id).Select(x => x.GroupId).FirstOrDefault();
}

查询返回的类型与方法定义的返回类型不匹配

更新方法以返回所需类型,在本例中为
int

public int GetGroupId (int id) {
    return  _context.Person.Where(c => c.Id == id).Select(x => x.GroupId).FirstOrDefault();
}

这将导致出现来自
FirstOrDefault
的空值问题,您还可以返回
int?
Person
这将导致出现来自
FirstOrDefault
的空值问题,您还可以返回
int?
Person