I have this block of code that eventually get serialized to JSON, for use in the Jquery FullCalender plugin. The ToUnixTimeSpan method taskes in a DateTime object and returns the number of seconds since 1970.
DateEnd could be null. In this block of code how do i test for the null and skip the end = ToUnixTimespan(e.DateEnd), if DateEnd is null? is there a C# equivalent to the groovy safe operator?
var listEvents = from e in eventRepository.GetAllEvents()
select new
{
id = e.EventID,
title = e.EventTitle,
start = ToUnixTimespan(e.DateStart),
end = ToUnixTimespan(e.DateEnd),
url = "/Events/Details/" + e.EventID
};
Further info about the ToUnixTimespanMethod:
private long ToUnixTimespan(DateTime date)
{
TimeSpan tspan = date.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0));
return (long)Math.Truncate(tspan.TotalSeconds);
}