使用时,我很难从一个函数中获取两个记录 采集。我不知道自己在做什么错,在此先感谢您 帮助。
服务调用功能...
Announcements announcements = new Announcements(ID, moduleID, _config, _logger);
return announcements; // this does not contain anything although Announcements load functions has two record
namespace Api.Collection
{
public class Announcements : List<Announcement>
{
IConfiguration _config;
ILoggerManager _logger;
private string _uspGetAnnouncement = "storedproced_GetAnnouncemnt";
public List<Announcement> announcements = new List<Announcement>();
public Announcements()
{
}
public Announcements(int employeeID, int moduleID, IConfiguration config, ILoggerManager logger)
{
_config = config;
_logger = logger;
if (employeeID > 0 && moduleID > 0)
{
Load(employeeID, moduleID);
}
}
public virtual void Load(int employeeID, int moduleID)
{
List<SqlParameter> lParam = new List<SqlParameter>();
Util.DataUtil dataUtil = new Util.DataUtil(_config);
SqlParameter param;
if (employeeID > 0 && moduleID > 0)
{
param = new SqlParameter("@employeeID", employeeID);
lParam.Add(param);
param = new SqlParameter("@moduleID", moduleID);
lParam.Add(param);
}
DataTable dt = Util.GetDataTable(_uspGetAnnouncement, lParam.ToArray());
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
announcements.Add(new Announcement(dr, dt.Rows.Count, _config, _logger));
}
}
return announcements; *** this has 2 records
}
}
}
答案 0 :(得分:0)
朋友评论可能是正确的,但我不知道您的用例,因此也许是有效的。 就像其他评论/回答中所述,Load方法不返回任何内容,因此您的行
return announcements;
什么都不做。 您实际上是将项目添加到您拥有的内部列表中。 因此,我猜测您当前的代码,这将有助于向您显示服务代码中的两个项目。
Announcements announcements = new Announcements(ID, moduleID, _config, _logger);
return announcements.announcements;
显然,这看起来令人困惑,而且不是您想要的。由于您是从List继承而来,因此您需要在Load函数中将各项添加到自身中。因此,我相信以下代码会起作用。关键是只使用“添加”而不是“公告”。添加
namespace Api.Collection
{
public class Announcements : List<Announcement>
{
IConfiguration _config;
ILoggerManager _logger;
private string _uspGetAnnouncement = "storedproced_GetAnnouncemnt";
public Announcements()
{
}
public Announcements(int employeeID, int moduleID, IConfiguration config, ILoggerManager logger)
{
_config = config;
_logger = logger;
if (employeeID > 0 && moduleID > 0)
{
Load(employeeID, moduleID);
}
}
public virtual void Load(int employeeID, int moduleID)
{
List<SqlParameter> lParam = new List<SqlParameter>();
Util.DataUtil dataUtil = new Util.DataUtil(_config);
SqlParameter param;
if (employeeID > 0 && moduleID > 0)
{
param = new SqlParameter("@employeeID", employeeID);
lParam.Add(param);
param = new SqlParameter("@moduleID", moduleID);
lParam.Add(param);
}
DataTable dt = Util.GetDataTable(_uspGetAnnouncement, lParam.ToArray());
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
Add(new Announcement(dr, dt.Rows.Count, _config, _logger));
}
}
}
}
}
答案 1 :(得分:-1)
不确定是否可行。
namespace Api.Collection
{
public class Announcements : List<Announcement>
{
IConfiguration _config;
ILoggerManager _logger;
private string _uspGetAnnouncement = "storedproced_GetAnnouncemnt";
public List<Announcement> announcements = new List<Announcement>();
public Announcements()
{
}
public Announcements(int employeeID, int moduleID, IConfiguration config, ILoggerManager logger)
{
_config = config;
_logger = logger;
if (employeeID > 0 && moduleID > 0)
{
var value = Load(employeeID, moduleID);
}
}
public virtual List<Announcement> Load(int employeeID, int moduleID)
{
List<SqlParameter> lParam = new List<SqlParameter>();
Util.DataUtil dataUtil = new Util.DataUtil(_config);
SqlParameter param;
if (employeeID > 0 && moduleID > 0)
{
param = new SqlParameter("@employeeID", employeeID);
lParam.Add(param);
param = new SqlParameter("@moduleID", moduleID);
lParam.Add(param);
}
DataTable dt = Util.GetDataTable(_uspGetAnnouncement, lParam.ToArray());
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
announcements.Add(new Announcement(dr, dt.Rows.Count, _config, _logger));
}
}
return announcements; *** this has 2 records
}
}
}
我希望澄清一下。...
问候