0

私はこのコードを持っています:

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;
      }
    }
  }
}

誰かが私が間違っていることを教えてもらえますか?ありがとう

4

4 に答える 4

1

これを試して:

.AddRange

あなたの議論は列挙可能のようです

于 2012-11-15T07:31:17.920 に答える
1

交換:

moduleDetail.Add(moduleId,title);

に:

moduleDetail.Add(new UserTrainingModules {userTrainingModuleId  = moduleId, title = title});
于 2012-11-15T07:33:48.633 に答える
0
public List<UserTrainingModules> GetTrainingModulesForUser(int userId)
{
  return UserRepository.GetTrainingModulesForUser(userId);
}
于 2012-11-15T07:35:19.377 に答える
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;
    }
于 2012-11-15T07:46:59.380 に答える