HIDORI on The Web

マネージコードしか書きたくない

勝手に競演

without comments

ネタ元: 夢の競演みたび

そのままだと面白くないので、複数除外できるようにしてみた。

簡素さを優先するとこんな感じ。excludes を何度も舐めるのが美しくないけど。

result を何度も列挙するような場合、result.ToArray() すればいいよね。

1回しか回さないなら、これでおk。

using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var source = new[]
            {
                new { Name = "しゅうたん",   Type = "アメショ" },
                new { Name = "ろり",         Type = "アメショ" },
                new { Name = "みずきちゃん", Type = "スコティ" },
                new { Name = "マグさん",     Type = "ほげ" },
                new { Name = "?",           Type = "もげ" },
            };

            var excludes = new[] { "ほげ", "もげ" };

            var result = source.Where(_ => !excludes.Contains(_.Type));

            foreach (var item in result)
            {
                System.Console.Out.WriteLine("Name = {0}, Type = {1}", item.Name, item.Type);
            }
        }
    }
}

Written by Hiroaki SHIBUKI

5月 27th, 2010 at 12:38 am

Posted in 技術情報

Tagged with ,

Leave a Reply