简体   繁体   English

ASP.Net MVC Linq to Sql如何处理null datetime

[英]ASP.Net MVC Linq to Sql how to handle a null datetime

I have a Topic parent table, and a Post table childed to the Topic table. 我有一个Topic父表,一个Post表childed到Topic表。

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: 我在Linq查询中尝试做的是从链接的Post表返回最后一个发布日期,但是,如果没有Posts,则下面的查询失败,因为DateTime不可为空:

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: 我的ViewModel是:

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? ...如果DateOfPost为null,它不会出错?

You can make your property Nullable 您可以使您的财产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 问号确保您可以在nullableDate属性中使用Null

You could use .GetValueOrDefault() to specify a default value if there is a null value: 如果存在空值,则可以使用.GetValueOrDefault()指定默认值:

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

Alternatively, you could make LastPostDate nullable in your model: 或者,您可以在模型中使LastPostDate可为空:

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. 我通常不在视图模型中使用可空类型,并尽可能设置默认值。

eIf the DateOfPost column in the databse is nullable then the DateTime in your entity should also be nullable as should your viewmodel Property. e如果数据库中的DateOfPost列可以为空,那么实体中的DateTime也应该是可视的,您的viewmodel属性也应该是可空的。 Alternatively if you don't want null in your view model you can us null coalescer 或者,如果您不希望在视图模型中使用null,则可以使用null coalescer

t.Posts.OrderByDescending(x => x.DateOfPost).FirstOrDefault().DateOfPost ?? DefaultDate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM