I have a foreach loop that I would like to replace with a Linq query, but I've not been able to figure out how to write the query. Please see my example below and TIA.
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication
{
class ExampleProgram
{
static void Main( string[] args )
{
Device device = new Device();
// The goal is to populate this list.
var list1 = new List<IMemory>();
// I would like to replace this loop with a Linq query
foreach( MemoryBank memoryBank in device.MemoryBanks )
{
list1.Add( memoryBank ); // add the memory bank
foreach( MemoryBlock memoryBlock in memoryBank.MemoryBlocks )
list1.Add( memoryBlock ); // add the memory block
}
//var list2 = from memoryBank in device.MemoryBanks
// from memoryBlock in memoryBank.MemoryBlocks
// select ???;
}
}
interface IMemory
{}
public class Device
{
public IList<MemoryBank> MemoryBanks { get; set; }
}
public class MemoryBank : MemoryBlock, IMemory
{
public IList<MemoryBlock> MemoryBlocks { get; set; }
}
public class MemoryBlock : IMemory
{ }
}