Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4064470
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:51:35+00:00 2026-05-20T15:51:35+00:00

my code I need to grab the bottom element (most recent) entry into my

  • 0

my code
I need to grab the bottom element (most recent) entry into my database and see if it was over 1 hour ago, the type of ObservationTime is DateTime. I keep getting an error saying that “Sequence contains more than one element” for my var mWeathers. I think it has to do with the way I’m orderby descending but i cant figure it out. Thanks

    [OperationContract]
    public bool LeastOneHour()
    {

        DataClassesDBDataContext db = new DataClassesDBDataContext();
        var mWeathers = (from weathertable in db.WeatherTables 
                         orderby weathertable.ObservationTime descending 
                         select weathertable.ObservationTime).Single();
        DateTime lastTime = Convert.ToDateTime(mWeathers).AddHours(1);
        if ( lastTime <= DateTime.Now)
            return true;
        else
            return false;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:51:35+00:00Added an answer on May 20, 2026 at 3:51 pm

    Single() says there must be one element in the result, and if there are more than one, throw an exception. What you’re likely looking for is First(), or FirstOrDefault() if there’s a chance there will be no results at all (in which case null is returned)

    If you can be guaranteed that there will always be an element, try this:

    DateTime lastDate = db.WeatherTables.OrderByDescending (w => w.ObservationTime).Select(w => w.ObservationTime).First();
    
    return lastDate.AddHours(1) <= DateTime.Now;
    

    Otherwise something like

    DateTime? lastDate = db.WeatherTables.OrderByDescending (w => w.ObservationTime).Select(w => w.ObservationTime).FirstOrDefault();
    
    return lastDate.HasValue ? lastDate.AddHours(1) <= DateTime.Now : false;
    

    Assuming of course ObservationDate is declared as DateTime?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to grab values from the querystring and inject them into a getURL();
I need to grab the path from the registry. The following code works except
I need code in Perl for requesting and parsing ATOM and RSS feeds. Is
how to send rich text message in system.net.mail need code for send a mail
I have a console application that require to use some code that need administrator
HI, I m doing the folling stuff in the jsp code I need to
What code do you need to add in PHP to automatically have the browser
I have the following code that I need to add an additonal object to
With JPA I do not need to code the SQL for every new attribute
I have a C code base and need to programmatically identify all of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.