不能用集合初始化器初始化类型,因为它没有实现ienumerable
本文关键字:初始化 因为 实现 ienumerable 集合 类型 不能 | 更新日期: 2023-09-27 18:07:51
我是一个相当初级的c#开发人员,所以请原谅我,如果这是很容易的,但我得到这个错误" invalid initializer member declarator
"在这行代码下面:
foreach (DataRow p in data.Rows)
{
DBTrack top = new DBTrack()
/* ===> Error starts here */ {
track.TrackID = SQLDataHelper.GetGuid(dataReader, "TrackID");
track.TrackName = SQLDataHelper.GetString(dataReader, "TrackName");
track.ArtistName = SQLDataHelper.GetString(dataReader, "ArtistName");
track.AddedDate = SQLDataHelper.GetDateTime(dataReader, "AddedDate");
};
DBTrackData.Add(top);
}
有人能解释这意味着什么,我如何工作,并实现它来显示数据?
这是供您检查的完整方法:
public static List<DBTrack> GetAllTracksFromReaderDB(IDataReader dataReader)
{
if (DBTrackData == null)
{
DBTrack track = new DBTrack();
System.Data.DataTable data = new System.Data.DataTable();
List<DBTrack> daa = new List<DBTrack>();
DBTrackData = new List<DBTrack>();
foreach (DataRow p in data.Rows)
{
DBTrack top = new DBTrack()
/* ===> Error starts here */ {
track.TrackID = SQLDataHelper.GetGuid(dataReader, "TrackID");
track.TrackName = SQLDataHelper.GetString(dataReader, "TrackName");
track.ArtistName = SQLDataHelper.GetString(dataReader, "ArtistName");
track.AddedDate = SQLDataHelper.GetDateTime(dataReader, "AddedDate");
};
DBTrackData.Add(top);
}
}
return DBTrackData;
}
编辑:public Guid TrackID { get; set; }
public string TrackName { get; set; }
public string ArtistName { get; set; }
public DateTime AddedDate { get; set; }
任何帮助将是伟大的:)谢谢
您的语法对成员初始化无效。您不能实例化top
并尝试填充track
属性。
应该是这样的:
DBTrack top = new DBTrack
{
TrackID = SQLDataHelper.GetGuid(dataReader, "TrackID"),
TrackName = SQLDataHelper.GetString(dataReader, "TrackName"),
ArtistName = SQLDataHelper.GetString(dataReader, "ArtistName"),
AddedDate = SQLDataHelper.GetDateTime(dataReader, "AddedDate")
};