I'm trying to grab the first user from a SQL database using Entity model
that is talking to a already existing database that has a userID of the user I am looking for. This and a second error appear.
Cannot convert lambda expression to delegate type
System.Func<iomnientitylibrary.user,bool>
because some of the return types in the block are not implicitly convertible to the delegate return type.
Cannot implicitly convert type int
to bool
.
public user GetUser(int userID)
{
using (var context = new iomniEntities())
{
user u = context.users.FirstOrDefault(user => user.userID);
return u;
}
}
context.users.ToList()
is working properly but I don't want to be that inefficient.
When using the expression:user u = context.users.FirstOrDefault(user => user.userID);
the return type is of userID ( this is determined by the 2nd part of Lambda expression) and NOT of type: 'user' , which the statement expects as per the declaration: user u
So, if you want to return a single user
whose userID is 'userID', use:
user u = context.users.FirstOrDefault(user => user.userID==userID);
OR you can also use:
user u = context.users
.Where(user => user.UserId==UserID)
.Select(user => user).Single();
Also make sure you have the Using statement: using System.Linq;