Reputation: 179
I have 3 classes:
public class Disciplina
{
public int Key { get; set; }
public string Name { get; set; }
public string[] Semestr { get; set; }
public int Time { get; set; }
public List<int> TimeToAll { get; set; }
public string Otchetnost { get; set; }
public int Specialnost { get; set; }
...
}
public class Cafedra
{
public int Key { get; set; }
public string Name { get; set; }
public string Facultet { get; set; }
public string Telefone { get; set; }
public List<int> Specializations { get; set; }
...
}
public class Specialization
{
public int Key { get; set; }
public string Name { get; set; }
public string SpecialName { get; set; }
public string FormaObuch { get; set; }
public string[] DisplinsID { get; set; }
...
}
I need to choose all Disciplina of chosen cafedra. I created by many foreach but I need it with linq.
I tried, but one Cafedra can have a lot of Specialization, and the Specialization can have a lot of Disiplins and I do not know how it can be chosen in a LINQ?
My method
private static void DiscipliniCafedri()
{
Console.WriteLine("Выберите кафедру:");
for (int i = 0; i < Cafedras.Count; i++)
{
Console.WriteLine(i + 1 + " " + Cafedras[i].ToString());
}
int ID = Convert.ToInt32(Console.ReadLine());
List<Specialization> spesc = new List<Specialization>();
Console.Clear();
Console.WriteLine("Дисциплины выбранной кафедры:");
foreach (int s in Cafedras[ID - 1].Specializations)
{
spesc.Add(Specializations[s - 1]);
}
foreach (Specialization s in spesc)
{
foreach (string d in s.DisplinsID)
{
Console.WriteLine(Disciplinas[Convert.ToInt32(d) - 1].Name);
}
}
}
Upvotes: 2
Views: 411
Reputation: 726469
Converting nested loops does not require a join - you can do a sequence of Select
calls instead:
var res = Cafedras[ID - 1]
.Specializations
.Select(s => Specializations[s - 1])
.SelectMany(s => Disciplinas[Convert.ToInt32(s.DisplinsID) - 1].Name)
.ToList();
Above,
Select
represents the first loop that creates specs
SelectMany
represents the two nested loops.This produces List<string>
in the res
, with subject names that could be printed.
Upvotes: 2
Reputation: 56393
The LINQ equivalent would be:
var resultSet = Cafedras[ID - 1].Specializations
.Select(s => Specializations[s - 1])
.SelectMany(s => s.DisplinsID);
foreach(var d in resultSet)
Console.WriteLine(Disciplinas[Convert.ToInt32(d) - 1].Name);
Upvotes: 1