是否有可能取代使用LINQ这个foreach循环? [英] Is it possible to replace this foreach loop with Linq?

查看:190
本文介绍了是否有可能取代使用LINQ这个foreach循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个foreach循环,我想用LINQ查询来代替,但我没能弄清楚如何编写查询。请参见下面和TIA我的例子

 使用System.Collections.Generic; 
使用System.Linq的;

命名空间ConsoleApplication
{
类ExampleProgram
{
静态无效的主要(字串[] args)
{
设备装置=新的设备();

//的目的是填充此列表。
变种list1的=新的List< IMemory>();

//我想换掉这个循环使用LINQ查询
的foreach(MemoryBank memoryBank在device.MemoryBanks)
{
list1.Add(memoryBank); //添加存储库

的foreach(MemoryBlock中MemoryBlock中的memoryBank.MemoryBlocks)
list1.Add(MemoryBlock中); //添加内存块
}

// VAR列表2 =从memoryBank在device.MemoryBanks
//从MemoryBlock中的memoryBank.MemoryBlocks
//选择? ??;
}
}

接口IMemory
{}

公共类设备
{
公众的IList< MemoryBank> ; MemoryBanks {搞定;组; }
}

公共类MemoryBank:MemoryBlock中,IMemory
{
公众的IList< MemoryBlock中> MemoryBlocks {搞定;组; }
}

公共类MemoryBlock中:IMemory
{}
}


解决方案

您可以这样做:

  VAR的List1 = device.MemoryBanks 
.SelectMany(M = gt;新建[] {M-} .Concat(m.MemoryBlocks))
.ToList();



注意,这将创建一个列表< MemoryBlock中> 而非列表与LT; IMemory> 作为样本。为了使接口类型列表中,作出最后的通话了ToList< IMemory>()



编辑:



在.NET 3.5,其中的IEnumerable< T> 界面不是协变的,你可以这样做:

  VAR list1的= device.MemoryBanks 
.SelectMany(M = gt;新建IMemory [] {M-}
.Concat(m.MemoryBlocks.Cast< IMemory>()))
.ToList();


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 
    { }
}

解决方案

You can do:

var list1 = device.MemoryBanks
                  .SelectMany(m => new[] { m }.Concat(m.MemoryBlocks))
                  .ToList();

Do note that this will create a List<MemoryBlock> rather than a List<IMemory> as in your sample. To make the list of the interface type, make the final call ToList<IMemory>().

EDIT:

In .NET 3.5, in which the IEnumerable<T> interface is not covariant, you can do:

var list1 = device.MemoryBanks
                  .SelectMany(m => new IMemory[] { m }
                                       .Concat(m.MemoryBlocks.Cast<IMemory>()))
                  .ToList();

这篇关于是否有可能取代使用LINQ这个foreach循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆