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;
}
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 isFirst(), orFirstOrDefault()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:
Otherwise something like
Assuming of course ObservationDate is declared as DateTime?