asp.net mvc datettime linq检查为空

时间:2010-11-22 13:52:08

标签: asp.net-mvc linq

var postsidebar = from post in postrepository.GetAllPosts()
    join pstmt in postrepository.GetAllPostMetas()
    on post.int_PostId equals pstmt.int_PostId
    where (post.int_PostTypeId == 4 
        && post.int_PostStatusId == 2 
        && post.int_OrganizationId == layoutrep.GetSidebarDetailById(SidebarDetailsId).int_OrganizationId)
        && (pstmt.vcr_MetaKey.Contains(filter) && pstmt.vcr_MetaValue.Contains("true")     
        && (System.DateTime.Now >= 
            Convert.ToDateTime(pstmt.Post.PostMetas.FirstOrDefault(m => 
                m.vcr_MetaKey == "Publish Date").vcr_MetaValue))) 
    select post;

如何在日期中查看此部分中的空(显示错误)

&& (System.DateTime.Now >= Convert.ToDateTime(pstmt.Post.PostMetas.FirstOrDefault(m => 
    m.vcr_MetaKey == "Publish Date").vcr_MetaValue)))

2 个答案:

答案 0 :(得分:1)

你可以尝试先消除空值的可能性,然后再尝试你的演员。

&& pstmt.Post.PostMetas.FirstOrDefault(m => 
    m.vcr_MetaKey == "Publish Date"
    && !string.IsNullOrEmpty(m.vcr_MetaValue))
&& (System.DateTime.Now >= 
        Convert.ToDateTime(pstmt.Post.PostMetas.FirstOrDefault(m => 
            m.vcr_MetaKey == "Publish Date").vcr_MetaValue))) 

答案 1 :(得分:0)

试试这个:

// declare the action for re-use
Func<PostMeta,bool> action = m => m.vcr_MetaKey == "Publish Date";

// then test for Any() before comparing anything
&& (pstmt.Post.PostMetas.Any(action) && System.DateTime.Now >= Convert.ToDateTime(pstmt.Post.PostMetas.First(action).vcr_MetaValue)))