6

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.

1
  • 3
    There is no need for ToList here (that will not change the types required for subsequent Enumerable calls). This error is a compile-time type-error. Look at the expression/line with the "red squiggles". In particular, user => user.userID does not return a boolean value which is what the error is trying to say. (Not using ToList in certain EF scenarios is may lead to runtime errors, which are different than compile-time type-errors: it would not fix this issue.) Commented Jul 23, 2013 at 0:27

7 Answers 7

7

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;

Sign up to request clarification or add additional context in comments.

1 Comment

.Select(x => x) is a useless term, always
1

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;
    }
}

2 Comments

.Where(f).FirstOrDefault() and .FirstOrDefault(f) should result in equivalent (the same?) queries. In any case, both forms are typed the same so a type-problem (with f) affecting one will affect the other.
@user2246674 Yes. I had missed the Func<TSource, Boolean> aspect of both the .Where() and .FirstOrDefault()...edited.
1

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).

Comments

0

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

Comments

0

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);

Comments

0

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; }

}

Comments

-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();

1 Comment

All the existing answers say this. What is the added value of your answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.