繁体   English   中英

使用LINQ to SQL无法将null值加载到DateTime属性

[英]Unable to load a null value to a DateTime property with LINQ to SQL

我从数据库中获取数据并将其用作Repeater数据源有时我的日期值为空。

var mQuery = (from rmm in db.global_rawmaterial_mains
              where //bla bla bla 
              orderby rmm.name
              select new
              {
                  rmm.name, 
                  rmm.source,
                  date = rmm.global_rawmaterial_entries
                            .Where(p => p.date <= selectedDate)
                            .OrderByDescending(p => p.date)
                            .Select(p => (DateTime?)p.date) // here this date I want to convert .ToString("dd-MM-yyyy")
              });

我看到的日期如下: 2/9/2009 12:00:00 AM 在上面的查询中,如何将其转换为.ToString(); 由于我不能将其用作:

.Select(p => (DateTime?)p.date.ToString("dd-MM-yyyy")) //Cannot cast expression of type string to DateTime  

我不能使用:

 .Select(p => (string?)p.date.ToString())// Only non-nullable value type could be underlying of 'System.Nullable'

如果我尝试作为:

.Select(p =>p.date.ToString())

我收到错误:

Exception: The null value cannot be assigned to a member with type System.DateTime which is a non-nullable value type.
date = rmm.global_rawmaterial_entries
                            .Where(p => p.date <= selectedDate)
                            .OrderByDescending(p => p.date)
                            .Select(p => p.date)
.Select(d=>d.ToString());

暂无
暂无

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

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