1

I am getting a cast error here but I do not understand as to why.

protected bool isPlayerdb(string userName)
{
    try
    {
        Users adminUsers = from users in SoccerEntities.Users
                           where users.roleId == new Guid("ED85788D-72DA-4D0A-8D5E-B5378FC00592") 
                           && users.UserName == userName
                           select users;

        if (adminUsers != null)
            return true;
        else
            return false;
    }
    catch (Exception ex)
    {
        throw new EntityContextException("isPlayer failed.", ex);
    }
}

Error in debugger is:

Error 11 Cannot implicitly convert type 'System.Linq.IQueryable<soccerCmsDal.Users>' to 'soccerCmsDal.Users'. An explicit conversion exists (are you missing a cast?) C:\new code\UniteCms\UniteCms\soccerCmsDal\SocerDataAccess.cs 80 23 soccerCmsDal

1
  • What is the type of Users ? Commented Nov 6, 2015 at 6:48

2 Answers 2

10

If you need only one record use FirstOrDefault or SingleOrDefault:

Users adminUsers = (from users in SoccerEntities.Users
                           where users.roleId == new Guid("ED85788D-72DA-4D0A-8D5E-B5378FC00592") && users.UserName == userName
                           select users).FirstOrDefault();

If you need to check, if player exists, you can use Any:

bool exists = (from users in SoccerEntities.Users
                           where users.roleId == new Guid("ED85788D-72DA-4D0A-8D5E-B5378FC00592") && users.UserName == userName
                           select users).Any();
Sign up to request clarification or add additional context in comments.

Comments

3

You're asking to assign a sequence value to single entity variable. That's like trying to do this:

// Won't work for the same reason
string name = new List<string> { "foo", "bar" };

If you're just trying to find out whether there are any results, you can use Any - and there's an overload of that taking a predicate as an argument, so you don't need a query expression at all.

Additionally, any time you find yourself writing:

if (condition)
    return true;
else
    return false;

you can just use:

if (condition)

So we end up with:

protected bool isPlayerdb(string userName)
{
    try
    {
        return SoccerEntities
            .Users
            .Any(user => user.roleId == new Guid("ED85788D-72DA-4D0A-8D5E-B5378FC00592") 
                         && user.UserName == userName)
    }
    catch (Exception ex)
    {
        throw new EntityContextException("isPlayer failed.", ex);
    }
}

Note that:

  • Your method name violates .NET naming conventions - consider PlayerExists instead
  • Catching Exception is rarely a good idea, IME - consider catching more specific exceptions
  • I've changed the name of the variable used in the predicate from users (which was the range variable name in the query expression) to user, because the predicate acts on a single user. Naming matters for clear code - making it clear whether you're dealing with a single entity or multiple entities helps readability. (That confusion was what led you to the compile-time failure.)

3 Comments

this was already answered and ur answers is not the case the above was so no need to repeat
@kymberly: No idea what "is not the case above" means, but the existing answer does not mention the overload of Any which includes the predicate... and using that simplifies the code significantly. See the sample code I've now included.
@kymberly: Additionally, my answer tries to explain why you were getting the error you did, as well as how to fix it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.