我有这段代码:
public List<UserTrainingModules> GetTrainingModulesForUser(int userId)
{
List<UserTrainingModules> trainingModules = new List<UserTrainingModules>();
var modules = UserRepository.GetTrainingModulesForUser(userId);
trainingModules.Add(modules); //not correct error is "invalid arguments"
return trainingModules;
}
我的类型UserTrainingModules:
public class UserTrainingModules
{
public virtual int userTrainingModuleId { get; set; }
public virtual string title { get; set; }
}
我的方法GetTrainingModulesForUser:
public List<UserTrainingModules> GetTrainingModulesForUser(int userId)
{
using (SqlConnection conn = new SqlConnection(ZincModelContainer.CONNECTIONSTRING))
{
using (SqlCommand cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "[Zinc].[GetTrainingModulesForUser]";
SqlParameter param = new SqlParameter("@UserId", System.Data.SqlDbType.Int);
param.Value = userId;
cmd.Parameters.Add(param);
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
adapter.Fill(dt);
ICollection<UserTrainingModules> moduleDetail = new List<UserTrainingModules>(); //not sure what i must have here
foreach (DataRow row in dt.Rows)
{
var moduleId = Convert.ToInt32(row["TrainingModuleArtifactScormModules_TrainingModuleArtifactId"]); //this line not correct
var title = row["Title"]; //not correct either
moduleDetail.Add(moduleId,title); //error is no overload method "add" takes 2 arguments
}
return moduleDetail;
}
}
}
}
有人可以告诉我,我做错了吗?
感谢
答案 0 :(得分:1)
试试这个:
.AddRange
你的论点似乎是一个可枚举的
答案 1 :(得分:1)
替换:
moduleDetail.Add(moduleId,title);
要:
moduleDetail.Add(new UserTrainingModules {userTrainingModuleId = moduleId, title = title});
答案 2 :(得分:0)
public List<UserTrainingModules> GetTrainingModulesForUser(int userId)
{
return UserRepository.GetTrainingModulesForUser(userId);
}
答案 3 :(得分:0)
像这样使用AddRange
public List<UserTrainingModules> GetTrainingModulesForUser(int userId)
{
List<UserTrainingModules> trainingModules = new List<UserTrainingModules>();
var modules = UserRepository.GetTrainingModulesForUser(userId);
trainingModules.AddRange(modules); // It would work fine
return trainingModules;
}