3

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?

1
  • 1
    Use nullable type: DateTime? Commented Jun 20, 2013 at 7:35

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

I thought I had tried that!! - Thank you. (I'll mark as answer when SO allows)
1

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.

2 Comments

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
Hi, sorry about that, I have made a change which should stop this error.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.