Cannot convert lambda expression to delegate type

entity-framework linq sql visual-studio-2010

Question

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
2
4/3/2019 11:51:57 PM

Popular Answer

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;

6
7/23/2013 3:06:08 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow