Skip to content

Instantly share code, notes, and snippets.

@ryz310
Created May 16, 2013 11:10
Show Gist options
  • Save ryz310/5590990 to your computer and use it in GitHub Desktop.
Save ryz310/5590990 to your computer and use it in GitHub Desktop.
LINQとdynamic使ったサンプル。やっぱLINQ使うならdynamicは必須だね。 C#は今後も良い感じに進化していってほしい。
using System;
using System.Collections.Generic;
using System.Linq;
namespace CExperiments
{
public class Program
{
public static void Main ()
{
var sample = LinqSample ();
foreach (var s in sample) {
Console.WriteLine ("{0} : {1} {2}", s.Key, s.Value, s.Data);
}
/*
* 1 : a 111
* 3 : c 333
* 5 : e 555
*/
}
public static IEnumerable<dynamic> LinqSample()
{
var data1 = new [] {
new { Key = 1, Value = "a" },
new { Key = 2, Value = "b" },
new { Key = 3, Value = "c" },
new { Key = 4, Value = "d" },
new { Key = 5, Value = "e" },
new { Key = 6, Value = "f" },
};
var data2 = new [] {
new { Key = 1, Data = "111" },
new { Key = 2, Data = "222" },
new { Key = 3, Data = "333" },
new { Key = 4, Data = "444" },
new { Key = 5, Data = "555" },
new { Key = 6, Data = "666" },
};
return
from
a in data1
join
b in data2 on
a.Key equals b.Key
where
a.Key % 2 == 1
select
new { a.Key, a.Value, b.Data };
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment