我不知道我在这里做错了什么,还是真的做错了什么。
这可能完全是一个忽视,这就是为什么我把它送到社区去看一看。
我正在客户端创建一个List of Tuple<T,Boolean>,并通过REST返回到WCF。我在服务器端得到的值非常好。但是,当我试图在for each循环中使用它们时,这些项将显示为NULL并出错。
有趣的是,我用经典的for each循环替换了for循环,代码工作得很好。
下面是我正在使用的代码。另外附加的是图像,它包含了我在这里放置的场景的细节。
//This code fails
foreach (Tuple<Candidate, Boolean> cand in candidateList) //candidateList has got items perfectly in it.
{
Candidate cd = cand.Item1; //cand comes out as NULL
if (cd.IsShortlisted)
InsertShortCandidates(jobPost.JobID.ToString(), cd.UserID, cd.MatchingScore.ToString());
else
RemoveShortCandidates(jobPost.JobID.ToString(), cd.UserID);
}
//This code runs perfectly fine
for (Int32 idx = 0; idx < candidateList.Count; idx++) // As expected candidateList has got items.
{
Tuple<Candidate, Boolean> cand = candidateList[idx]; // Here cand has got values good to be used further.
Candidate cd = cand.Item1;
if (cd.IsShortlisted)
InsertShortCandidates(jobPost.JobID.ToString(), cd.UserID, cd.MatchingScore.ToString());
else
RemoveShortCandidates(jobPost.JobID.ToString(), cd.UserID);
}

发布于 2018-06-20 05:32:13
您可以通过这种方式轻松地迭代元组,假设我有一个类。
类
public class MyClass
{
public int Counter { get; set; }
}元组列表
List<Tuple<MyClass, bool>> listoftuple = new List<Tuple<MyClass, bool>>();元组集列表
for (int i = 0; i <=10; i++)
{
MyClass cls = new MyClass();
cls.Counter = i;
Tuple<MyClass, bool> tuple =
new Tuple<MyClass, bool>(cls, true);
listoftuple.Add(tuple);
}获得元组值
foreach (Tuple<MyClass, bool> item in listoftuple)
{
Tuple<MyClass, bool> gettuple = item;
Console.WriteLine(gettuple.Item1.Counter);
Console.WriteLine(gettuple.Item2);
}问题与代码
代码中的问题是foreach循环的第一行。
候选cd = cand.Item1;
试着用
Tuple<Candidate, Boolean> cd = cand然后
Candidate candidate = cd.Item1https://stackoverflow.com/questions/50940375
复制相似问题