Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a Topic parent table, and a Post table childed to the Topic table.

What I'm trying to do within the Linq query is return the last post date, from the linked Post table, however, if there are no Posts, then the query below fails, as DateTime is not nullable:

The cast to value type 'DateTime' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.

The query is:

var topic = db.Topics.Include(x => x.Posts).Include(x => x.Forum).Where(x => x.ForumId==id)
           .Select(t => new TopicViewModel
             {
                 TopicId =t.TopicId,
                 ForumId=t.ForumId,
                 Title=t.Title,
                 DateOfTopic=t.DateOfPost,
                 Replies=t.Posts.Count()-1,
                 Author=t.Author,
                 Views = t.Views,
                 LastPost = t.Posts.OrderByDescending(x => x.DateOfPost).FirstOrDefault().Author,
                 LastPostDate = t.Posts.OrderByDescending(x => x.DateOfPost).FirstOrDefault().DateOfPost
             }).OrderByDescending(x=> x.DateOfTopic).ToList();

My ViewModel is:

public class TopicViewModel
{
    public int TopicId { get; set; }
    [Required]
    public string Title { get; set; }
    public string Author { get; set; }
    public DateTime DateOfTopic { get; set; }
    public int Replies { get; set; }
    public int Views { get; set; }
    public string LastPost { get; set; }
    public DateTime LastPostDate { get; set; }
    public int ForumId { get; set; }
}

Is there anyway of changing this line:

LastPostDate = t.Posts.OrderByDescending(x => x.DateOfPost).FirstOrDefault().DateOfPost

...so that it doesn't error if DateOfPost is null?

share|improve this question
1  
Use nullable type: DateTime? –  MarcinJuraszek Jun 20 '13 at 7:35
add comment

3 Answers

up vote 1 down vote accepted

You can make your property Nullable

public class x {
public DateTime? nullableDate {get; set;}
}

This should fix your issue. The questionmark makes sure you can have Null in the nullableDate property

share|improve this answer
    
I thought I had tried that!! - Thank you. (I'll mark as answer when SO allows) –  user1041617 Jun 20 '13 at 7:42
    
Thank you. You can already accept answers if they solved your issue or upvote them if they were helpful to you. –  Tikkes Jun 20 '13 at 7:44
add comment

You could use .GetValueOrDefault() to specify a default value if there is a null value:

LastPostDate = t.Posts
    .OrderByDescending(x => x.DateOfPost)
    .AsEnumerable()
    .FirstOrDefault()
    .DateOfPost.GetValueOrDefault(DateTime.MinValue);

Alternatively, you could make LastPostDate nullable in your model:

public class TopicViewModel
{
    public int TopicId { get; set; }
    [Required]
    public string Title { get; set; }
    public string Author { get; set; }
    public DateTime DateOfTopic { get; set; }
    public int Replies { get; set; }
    public int Views { get; set; }
    public string LastPost { get; set; }
    public DateTime? LastPostDate { get; set; }
    public int ForumId { get; set; }
}

I normally don't use nullable types in my view models, and set default values where possible.

share|improve this answer
    
Hi - that gives the error 'System.DateTime' does not contain a definition for 'GetValueOrDefault' and no extension method 'GetValueOrDefault' accepting a first argument of type 'System.DateTime' could be found' - thanks –  user1041617 Jun 20 '13 at 7:40
    
Hi, sorry about that, I have made a change which should stop this error. –  Oliver Jun 20 '13 at 7:42
add comment

eIf the DateOfPost column in the databse is nullable then the DateTime in your entity should also be nullable as should your viewmodel Property. Alternatively if you don't want null in your view model you can us null coalescer

t.Posts.OrderByDescending(x => x.DateOfPost).FirstOrDefault().DateOfPost ?? DefaultDate
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.