.NET 2.x System information — how to improve the code?

string savePath = @"C:\Sys.txt";
 using (FileStream file = new FileStream(savePath, FileMode.Append))
{
 using (StreamWriter twf = new StreamWriter(file, Encoding.UTF8))
{
 foreach (ManagementBaseObject avResult in avsearcher.Get())
{
 pcAntivirus = "& " + (avResult.GetPropertyValue("displayName")).ToString();
 pcAntivirus = (pcAntivirus.StartsWith("& ")) ? pcAntivirus.Substring(1) : pcAntivirus;
 twf.WriteLine(" security: "+ pcAntivirus);
}
 foreach (ManagementObject in gr gs.Get())
{
 foreach (PropertyData pty in gr.Properties)
{
 if (pty.Name == "Description")
{
 pcGPU += pty.Value.ToString();
 twf.WriteLine(" display Adapter: "+ pcGPU);
}
}
}
foreach (ManagementObject gob in the sar.Get())
{
 twf.WriteLine(" computer Model: "+ gob["Manufacturer"] + gob["Model"]);
 }


The problem is that too many foreach ! how can I reduce all this?
July 4th 19 at 22:52
1 answer
July 4th 19 at 22:54
How to use LINQ SelectMany and its method (perhaps several times). (see professorweb.ru/my/LINQ/base/level2/2_2.php)
And Concat methods.
Ie will be something like this
IEnumerable<string> strings = avsearcher.Get().Select(avResult => avResult.ToDisplayName()) // this extension method that does the desired string
.Concat(
 gs.Get().SelectMany(gr => gr.Properties).Where(prt => prt.Name == "Description").Select(pry => prt.Value.ToString()) // here also you can use the extension
).Concat(
 sar.Get().Select(gob => gob.ToDisplayName()) // also method extension
 ); // etc.
// well, then you can even using foreach or any other way to record in twf</string>

Find more questions by tags C#