Jonas Willander
Jonas Willander

Reputation: 528

Using Linq to return groupId

I have this class :

public class Person
{
   public int Id { get; set; }
   public string UserName { get; set; }
   public int GroupId { get; set; }     
}

How do I do if I want to return GroupId if I have given Id .. like

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

But this giving me error message like "cannot implicitly convert type".

Upvotes: 0

Views: 58

Answers (1)

Nkosi
Nkosi

Reputation: 247323

The type returned by the query does not match the return type defined by the method.

Update the method to return the desired type, which in this case is int

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

Upvotes: 3

Related Questions