How to return false if there are no records in the table

Want to check if there are any entries in the ClientAccessCode table if you don't return false ...

if (!CheckAccessCodeExists())
{
    Console.WriteLine("Client Access code does not exist");
    throw new ConfigurationErrorsException("Client Access code does not exist");
}

private static bool CheckAccessCodeExists()
{
    using (EPOSEntities db = new EPOSEntities())
    {
        ClientAccountAccess clientAccess = db.ClientAccountAccesses
                .OrderByDescending(x => x.Id)
                .Take(1)
                .Single();

        if (clientAccess != null)
        {
            return true;
        }
        return false;
    }
}

// this labeled sequence contains no elements in the lamba expression, so how can I just return false? Is some use of .Any () possible?

thank

EDIT Thats thank you very much for helping the guys il to mark the answer now, also another request if I had

ClientAccountAccess clientAccess = db.ClientAccountAccesses
                  .OrderByDescending(x => x.Id)
                  .Take(1)
                  .Single();

if (clientAccess != null)
{
    db.DeleteObject(clientAccess);
}

how can i reorganize this to say something neater like

if (db.ClientAccountAccesses.Any())
{
    db.DeleteObject(//what does in here do I have to use above code to get record to delete?);
}
+3
source share
3 answers
private static bool CheckAccessCodeExists()
    {
        using (EPOSEntities db = new EPOSEntities())
        {
            var item = db.ClientAccountAccesses.FirstOrDefault();
            if(item != null)
            {
               db.Remove(item);
               db.SaveChanges(); 
               return true;                 
            }

            return false;
        }
    }
0
source

Single , , 1, . , SingleOrDefault - , , null.

ClientAccountAccess clientAccess = db.ClientAccountAccesses
    .OrderByDescending(x => x.Id)
    .Take(1)
    .SingleOrDefault();

if (clientAccess != null)
{
    db.DeleteObject(clientAccess);
}
0
    private static bool CheckAccessCodeExists()
    {
        using (EPOSEntities db = new EPOSEntities())
        {
            var item = db.ClientAccountAccesses.FirstOrDefault();
            if (item != null)
            {
                db.DeleteObject(item);
                db.SaveChanges();
                return true;
            }             
        }
        return false;
    }
-1
source

All Articles