我有一个foreach循环,想用Linq查询替换它,但我一直无法找到如何编写查询的方法。请看下面的示例并谢谢。
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
{ }
}