C# 可为空的日期时间转换
可能重复:C# 可为空的日期时间转换,c#,asp.net,sql,entity-framework,C#,Asp.net,Sql,Entity Framework,可能重复: 如果论坛没有lastPostDate,我试图将我的reader[3]对象datetime转换为null,但它说我缺少转换。 错误: 无法确定条件表达式的类型,因为在和'System.DateTime'之间没有隐式转换 公共类论坛 { 公共列表GetForums() { 使用(SqlConnection conn=new SqlConnection(ConfigurationManager.ConnectionString[“CMS”].ConnectionString)) { co
如果论坛没有lastPostDate,我试图将我的reader[3]对象datetime转换为null,但它说我缺少转换。 错误: 无法确定条件表达式的类型,因为在
和'System.DateTime'之间没有隐式转换
公共类论坛
{
公共列表GetForums()
{
使用(SqlConnection conn=new SqlConnection(ConfigurationManager.ConnectionString[“CMS”].ConnectionString))
{
conn.Open();
SqlCommand cmd=newsqlcommand(“存储过程\论坛\获取论坛”,康涅狄格州);
cmd.CommandType=CommandType.storedProcess;
SqlDataReader=cmd.ExecuteReader(CommandBehavior.Default);
列表论坛=新列表();
while(reader.Read())
{
var title=reader[6]。ToString();
var threadCount=(int)读取器[5];
var lastPostTitle=reader[4].ToString();
//下面是我遇到的问题
可为空的lastPostDate=(读取器[3]==DBNull.Value?null:Convert.ToDateTime(读取器[3]);
var lastPostBy=reader[2]。ToString();
var-forumGroup=reader[1]。ToString();
var description=读取器[0]。ToString();
Forum Forum=新论坛(0,”,DateTime.Now,
读卡器[“Title”].ToString(),说明,
0,false,,,DateTime。现在,true,
forumGroup,(int)线程数,lastPostBy,
lastPostDate,lastPostTitle);
论坛。添加(论坛)/**/
}
返回论坛;
}
}
}
下面是我的论坛类对象,lastPostDate可以为空
public class Forum
{
public Forum(int forumID, string addedBy, DateTime addedDate, string title, string description, int parentID, bool moderated,
string imageUrl, string updatedBy, DateTime? updatedDate, bool active, string forumGroup, int threadCount, string lastPostBy,
Nullable<DateTime> lastPostDate, string lastPostTitle)
{
this.ForumID = forumID;
this.AddedBy = addedBy;
this.AddedDate = addedDate;
this.Title = title;
this.Description = description;
this.ParentID = parentID;
this.Moderated = moderated;
this.ImageUrl = imageUrl;
this.UpdatedBy = updatedBy;
this.UpdatedDate = updatedDate;
this.Active = active;
this.ForumGroup = forumGroup;
this.ThreadCount = threadCount;
this.LastPostBy = lastPostBy;
this.LastPostDate = lastPostDate;
this.LastPostTitle = lastPostTitle;
}
private int _forumID;
public int ForumID
{
get { return _forumID; }
set { _forumID = value; }
}
private string _addedBy;
public string AddedBy
{
get { return _addedBy; }
set { _addedBy = value; }
}
private DateTime _addedDate = DateTime.Now;
public DateTime AddedDate
{
get { return _addedDate; }
set { _addedDate = value; }
}
private string _title = "";
public string Title
{
get { return _title; }
set { _title = value; }
}
private string _description = "";
public string Description
{
get { return _description; }
set { _description = value; }
}
private int _parentID = 0;
public int ParentID
{
get { return _parentID; }
set { _parentID = value; }
}
private bool _moderated = false;
public bool Moderated
{
get { return _moderated; }
set { _moderated = value; }
}
private string _imageUrl = "";
public string ImageUrl
{
get { return _imageUrl; }
set { _imageUrl = value; }
}
private string _updatedBy = "";
public string UpdatedBy
{
get { return _updatedBy; }
set { _updatedBy = value; }
}
private DateTime? _updatedDate = null;
public DateTime? UpdatedDate
{
get { return _updatedDate; }
set { _updatedDate = value; }
}
private bool _active = false;
public bool Active
{
get { return _active; }
set { _active = value; }
}
private string _forumGroup = "";
public string ForumGroup
{
get { return _forumGroup; }
set { _forumGroup = value; }
}
private int _threadCount = 0;
public int ThreadCount
{
get { return _threadCount; }
set { _threadCount = value; }
}
private string _lastPostBy = "";
public string LastPostBy
{
get { return _lastPostBy; }
set { _lastPostBy = value; }
}
private Nullable<DateTime> _lastPosteDate = null;
public Nullable<DateTime> LastPostDate
{
get { return _lastPosteDate; }
set { _lastPosteDate = value; }
}
private string _lastPostTitle = "";
public string LastPostTitle
{
get { return _lastPostTitle; }
set { _lastPostTitle = value; }
}
}
公共课堂论坛
{
公共论坛(int-forumID、string-addedBy、DateTime-addedDate、string-title、string-description、int-parentID、bool-moderated、,
字符串imageUrl、字符串UpdateBy、DateTime?UpdateDate、bool active、字符串forumGroup、int threadCount、字符串lastPostBy、,
可为空的lastPostDate,字符串lastPostTitle)
{
this.ForumID=ForumID;
this.AddedBy=AddedBy;
this.addedate=addedate;
这个.Title=Title;
这个。描述=描述;
this.ParentID=ParentID;
这个.缓和的=缓和的;
this.ImageUrl=ImageUrl;
this.UpdatedBy=UpdatedBy;
this.updateDate=updateDate;
这个。主动=主动;
this.ForumGroup=ForumGroup;
this.ThreadCount=ThreadCount;
this.LastPostBy=LastPostBy;
this.LastPostDate=LastPostDate;
this.LastPostTitle=LastPostTitle;
}
私人国际论坛;
公共国际论坛
{
获取{return\u forumID;}
设置{u forumID=value;}
}
私有字符串_被加上;
公共字符串加法
{
获取{return\u addedBy;}
设置{u addedBy=value;}
}
private DateTime _addedate=DateTime.Now;
公共日期时间加法
{
获取{return\u addedate;}
设置{u addedate=value;}
}
私有字符串_title=“”;
公共字符串标题
{
获取{return\u title;}
设置{u title=value;}
}
私有字符串_description=“”;
公共字符串描述
{
获取{return\u description;}
设置{u description=value;}
}
private int_parentID=0;
公共int父ID
{
获取{return\u parentID;}
设置{u parentID=value;}
}
private bool _moderated=假;
公众舆论缓和
{
获取{return\u mediated;}
设置{u mediated=value;}
}
私有字符串_imageUrl=“”;
公共字符串ImageUrl
{
获取{return\u imageUrl;}
设置{u imageUrl=value;}
}
私有字符串_updatedBy=“”;
公共字符串由更新
{
获取{return\u updatedBy;}
设置{u updatedBy=value;}
}
私有日期时间?\u updateDate=null;
公共日期时间?更新日期
{
获取{return\u updateDate;}
设置{u updateDate=value;}
}
私有bool _active=false;
公共图书馆
{
获取{return\u active;}
设置{u active=value;}
}
私有字符串_forumGroup=“”;
公共字符串组
{
获取{return\u forumGroup;}
设置{u forumGroup=value;}
}
私有int_threadCount=0;
公共整数线程数
{
获取{return\u threadCount;}
设置{u threadCount=value;}
}
私有字符串_lastPostBy=“”;
公共字符串LastPostBy
{
获取{return\u lastPostBy;}
设置{u lastPostBy=value;}
}
私有可空_lastPosteDate=null;
公共可为空的LastPostDate
{
获取{return\u lastPosteDate;}
设置{u lastPosteDate=value;}
}
私有字符串_lastPostTitle=“”;
公共字符串LastPostTitle
{
获取{return\u lastPostTitle;}
设置{u lastPostTitle=value;}
}
}
确保这两种类型是可为空的日期时间
var lastPostDate = reader[3] == DBNull.Value ?
null :
(DateTime?) Convert.ToDateTime(reader[3]);
- 使用
DateTime?
而不是Nullable
可以节省时间
- 使用更好的缩进?像我一样的表情
我在Eric Lippert中找到了这一极好的解释:
?:
运算符的规格说明如下:
?运算符的第二个和第三个操作数控制
条件表达式。设X和Y是第二个和第二个的类型
第三个操作数。那么
- 如果X和Y是同一类型,则这是条件的类型
表情
- 否则,如果存在从X到Y的隐式转换,
但不是从Y到X,那么Y是条件表达式的类型。
var lastPostDate = reader[3] == DBNull.Value ?
null :
(DateTime?) Convert.ToDateTime(reader[3]);
bool b = GetSomeBooleanValue();
object o = b ? "Forty-two" : 42;
object o = b ? "Forty-two" : (object)42;
object o = b ? (object)"Forty-two" : 42;
var lastPostDate = reader[3] == DBNull.Value ?
default(DateTime?):
Convert.ToDateTime(reader[3]);
DateTime? lastPostDate = (DateTime?)(reader.IsDbNull(3) ? null : reader[3]);