Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ从两个不同的列表中查找和打印值_C#_Linq - Fatal编程技术网

C# 使用LINQ从两个不同的列表中查找和打印值

C# 使用LINQ从两个不同的列表中查找和打印值,c#,linq,C#,Linq,我很难理解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 =>

我很难理解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("");
}
我有两个问题。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,然后选择进入一个列表,在运行中进行格式化并将其添加到同伴列表(可能是什么)


很难理解你正在做什么或想要实现什么

但是你需要改变你的逻辑,我想这样你才能进入这一集

关于你的第一个问题,如果你只想看第一集,你可以这样做

var episode = EpisodeList.FirstOrDefault(x => DoctorList.Any(y => y.Debut == x.Story));
if (episode != null)
{
   Debug.WriteLine($"year = {episode.Year}, Title = {episode.Title}");
}
关于你的第二个问题,如果你有多次出现,你可以只做一个where,然后选择进入一个列表,在运行中进行格式化并将其添加到同伴列表(可能是什么)


我将首先使用LINQ过滤相关对象

如果您知道最多有1个匹配的对象,则可以使用SingleOrDefault(如果找到该对象,则返回该对象)或默认值(对象为null):

Episode e = EpisodeList.SingleOrDefault(x => DoctorList.Any(y => y.Debut == x.Story))
if (e != null) // found episode
{
    Year.Text = e.Year.ToString();
    Episode.Text = e.Title;
}
如果可以有多个结果,您可以在以下位置使用:

List<Episode> es = EpisodeList.Where(x => DoctorList.Any(y => y.Debut == x.Story)).ToList();
foreach (Episode e in es)
{
    Companions.Items.Add(String.Format(e.Title + " (" + e.Year + ")"));
    Companions.Items.Add("");
}
额外信息
在LINQ到SQL中,将使用延迟求值,因此“ToList()”强制它立即从数据库中获取结果。这不是等待结果被使用(在本例中,通过foreach循环迭代)。但是,如果不使用LINQ to SQL,则可以忽略此差异。

我将首先使用LINQ筛选相关对象

如果您知道最多有1个匹配的对象,则可以使用SingleOrDefault(如果找到该对象,则返回该对象)或默认值(对象为null):

Episode e = EpisodeList.SingleOrDefault(x => DoctorList.Any(y => y.Debut == x.Story))
if (e != null) // found episode
{
    Year.Text = e.Year.ToString();
    Episode.Text = e.Title;
}
如果可以有多个结果,您可以在以下位置使用:

List<Episode> es = EpisodeList.Where(x => DoctorList.Any(y => y.Debut == x.Story)).ToList();
foreach (Episode e in es)
{
    Companions.Items.Add(String.Format(e.Title + " (" + e.Year + ")"));
    Companions.Items.Add("");
}
额外信息
在LINQ到SQL中,将使用延迟求值,因此“ToList()”强制它立即从数据库中获取结果。这不是等待结果被使用(在本例中,通过foreach循环迭代)。但如果不使用LINQ to SQL,则可以忽略此差异。

这很有帮助,但无法满足我的需要。它是一个列表框。而且输出不是我所期望的。它打印“x.Name(角色名称)@ZachLucas更新了答案以反映您的期望这很有帮助,但没有得到我所需要的。同伴是一个列表框。而且输出不是我所期望的。它打印“x.Name(角色名称)@ZachLucas更新了答案以反映您的期望