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

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100

2 Answers2

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();
Backs
  • 24,430
  • 5
  • 58
  • 85
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.)
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • this was already answered and ur answers is not the case the above was so no need to repeat – c-sharp-and-swiftui-devni Nov 06 '15 at 06:54
  • @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. – Jon Skeet Nov 06 '15 at 06:56
  • @kymberly: Additionally, my answer tries to explain *why* you were getting the error you did, as well as how to fix it. – Jon Skeet Nov 06 '15 at 06:58