我无法理解 LINQ 语句。我必须遵循以下代码:
//All list are filled from a file//
if (DoctorList.Any(y => EpisodeList.Any(x => y.Debut == x.Story)))
{
Year.Text = x.Year.ToString();
Episode.Text = x.Title;
}
if (DoctorList.Any(y => CompanionList.Any(c => y.docNum == c.Doctor)))
{
Companions.Items.Add(String.Format(c.Name + " (" + c.Actor + ")"));
}
if (CompanionList.Any(y => EpisodeList.Any(x => x.Story == y.Debut)))
{
Companions.Items.Add(String.Format(x.Title + " (" + x.Year + ")"));
Companions.Items.Add("");
}
我有 2 个问题。1 - 我需要打印找到的对象的值。例如,在下面的代码中,我需要获取存储在对象 x 中的名为 Year 的值和值 Title。
if (DoctorList.Any(y => EpisodeList.Any( x => y.Debut == x.Story)))
{
Year.Text = x.Year.ToString();
Episode.Text = x.Title;
}
2 - 与第二个和第三个 if 语句一起使用。可能有 30 个不同的同伴应该添加到列表框,但是对于那些 if 语句,我只能找到一个。我仍在学习如何使用 LINQ 语句,我不确定如何使用一个值获得多个值。
很难理解你在做什么或想要实现什么
但我认为你需要颠倒你的逻辑,这样你才能访问这一集
关于你的第一个问题,如果你只想访问发现的第一集,你可以做这样的事情
var episode = EpisodeList.FirstOrDefault(x => DoctorList.Any(y => y.Debut == x.Story));
if (episode != null)
{
Debug.WriteLine($"year = {episode.Year}, Title = {episode.Title}");
}
关于你的第二个问题,如果你有多次出现,你可以做一个 where,然后选择一个列表,即时进行格式化并将其添加到 Companions List(可能是什么)
var namesActors = EpisodeList.Where(x => DoctorList.Any(y => y.docNum == x.Doctor))
.Select(x => $"{x.Name} ({x.Actor})")
.ToList();
foreach (var value in namesActors)
Companions.Items.Add(value);
var titleYears = EpisodeList.Where(x => DoctorList.Any(y => y.Debut == x.Story))
.Select(x => $"{x.Title} ({x.Year})")
.ToList();
foreach (var value in titleYears)
Companions.Items.Add(value);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句