我将ICollection定义为
public virtual ICollection<Attempt> Attempts { get; set; }
Attempt
定义为
public class Attempt
{
public Guid Id { get; set; }
public string AttemptsMetaData { get; set; }
public DateTime Time { get; set; }
public bool Answered { get; set; }
public DateTime Disconnected { get; set; }
}
现在我想将ICollection传递给方法。
public void AddTask(ICollection<Attempt> attempts)
{
// Do something to the item in attempts.
}
现在我必须为此集合创建一个示例数据。我不知道怎么做。下面的代码可能有误。 我的想法是
// test data
Guid id = new Guid();
string attempt = "test";
DateTime dt = DateTime.Now.AddDays(-1);
bool answered = true;
DateTime disconnected = DateTime.Now;
ICollection <Attempt> attempts= new List<Attempt>();
// try to add sample data to the list
答案 0 :(得分:3)
List<Attempt> attempts= new List<Attempt>();
//Create a Instance of Attempt
Attempt objAtt = new Attempt();
Guid id = new Guid();
objtAtt.Id =id;
objtAtt.Time = DateTime.Now;
objAtt.AttemptsMetaData ="test";
objAtt.Answered = true;
objAtt.Disconnected = DateTime.Now;
attempts.Add(objAtt);
在这里传递给方法,
AddTask(attempts);
你这样的方法,
public void AddTask(List<Attempt> attempts)
{
}
答案 1 :(得分:0)
var attempt = new Attempt(/*whatever here*/);
attempts.Add(attempt);
AddTask(attempts);
答案 2 :(得分:0)
你试试这个:
public void AddTask(Attempt objAtempt)
{
List<Attempt> attempts = new List<Attempt>();
objAtempt.Id = Guid.NewGuid();
objAtempt.Time = DateTime.Now;
objAtempt.AttemptsMetaData = "test";
objAtempt.Answered = true;
objAtempt.Disconnected = DateTime.Now;
attempts.Add(objAtempt);
}