ggcodes
ggcodes

Reputation: 2909

Cannot convert lambda expression to delegate type

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.

Upvotes: 6

Views: 58232

Answers (7)

Rodrigo Cury
Rodrigo Cury

Reputation: 11

Check properties of class 'user' are declared as public.

like this

public class user
{
    [Key]
    public int id { get; set; }
    public string name { get; set; }
    public string password { get; set; }

}

Upvotes: 0

sadis
sadis

Reputation: 63

Like others mentioned you need to adjust your code a bit to do the comparison. Anyway I would do it with Single/SingleOrDefault as your are expecting to get a single user who matches the user id provided.

user u = context.users.SingleOrDefault(u => u.userID == userID);

or just do the return

return context.users.SingleOrDefault(u => u.userID == userID);

Upvotes: 0

Ali Dashtebozorgi
Ali Dashtebozorgi

Reputation: 1

you have to filter the users based on "UserId" and then select the first one . so the query should be like this :

User user = context.Users.Where(x=>x.UserId == UserId).FirstOrDefualt();

Upvotes: -1

R.C
R.C

Reputation: 10565

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;

Upvotes: 7

SyntaxError
SyntaxError

Reputation: 1752

Your lambda is wrong, it needs to return a boolean and yours just returns the .userID.

You need to use some kind of comparison, like:

user => user.userID == 10

FirstOrDefault returns the first or default (null in your case) of the items that match the lambda expression. It does not return the first item in the list which is what I think you are wanting. Without knowing what the context.Users data type is, I can't tell you a better way to do this.

You could use something like:

user => user != null

Upvotes: 0

Mathieu Guindon
Mathieu Guindon

Reputation: 71217

The expression in your FirstOrDefault method isn't right - it wants an expression that returns a bool and you give it user => user.userID which returns an int.

Just rewrite it to account for the parameter you're passing in: user => user.userID == userID

That said, if there wouldn't be 2 users with the same ID you're probably better off with context.Users.SingleOrDefault(user => user.userID == userID).

Upvotes: 1

ethorn10
ethorn10

Reputation: 1899

I think you just have your syntax off a bit. Try:

public user GetUser(int intUserID)
{
     using (var context = new iomniEntities())
    {
        user u = context.users.Where(u => u.userID == intUserID).FirstOrDefault();

        return u;
    }
}

Or to hold onto your version, it just needs touched up:

public user GetUser(int intUserID)
{
     using (var context = new iomniEntities())
    {
        user u = context.users.FirstOrDefault(user => user.userID == intUserID);

        return u;
    }
}

Upvotes: 1

Related Questions