http mnow jp http mnow wankuma com http

  • Slides: 32
Download presentation
Linqその効果的な使い方 えムナウ (児玉宏之) http: //mnow. jp/ http: //mnow. wankuma. com/ http: //blogs. wankuma. com/mnow/ http:

Linqその効果的な使い方 えムナウ (児玉宏之) http: //mnow. jp/ http: //mnow. wankuma. com/ http: //blogs. wankuma. com/mnow/ http: //www. ailight. jp/blog/mnow/ わんくま同盟 東京勉強会 #17

Linq to Object をながめてみる IEnumerableメソッド C# のクエリ式の構文 Cast Group. By   from type i

Linq to Object をながめてみる IEnumerableメソッド C# のクエリ式の構文 Cast Group. By   from type i in numbers From … As … group … by Group … By … Into … group … by … into …   join … on … equals … into Group Join … In … On … … Group. Join   join … on … equals …   let … = … Order. By orderby … Order. By. Descending orderby … descending Select select Select. Many 複数の from 句。 Then. By orderby …, … Then. By. Descending orderby …, … descending Where where Visual Basic のクエリ式の構文 From x In , y In Where x. a = y. a Join … [As …]In … On … Let … = … Order By … Descending Select 複数の From 句。 Order By …, … Descending Where わんくま同盟 東京勉強会 #17

Linq to Object をながめてみる IEnumerableメソッド C# のクエリ式の構文 Visual Basic のクエリ式の構文 All 該当なし Aggregate …

Linq to Object をながめてみる IEnumerableメソッド C# のクエリ式の構文 Visual Basic のクエリ式の構文 All 該当なし Aggregate … Into All(…) Any 該当なし Aggregate … Into Any() Average 該当なし Aggregate … Into Average() Count 該当なし Aggregate … Into Count() Distinct 該当なし Distinct Long. Count 該当なし Aggregate … Into Long. Count() Max 該当なし Aggregate … Into Max() Min 該当なし Aggregate … Into Min() Skip 該当なし Skip. While 該当なし Skip While Sum 該当なし Aggregate … Into Sum() Take 該当なし Take. While 該当なし Take While わんくま同盟 東京勉強会 #17

Linq to Object をながめてみる var early. Bird. Query = string[] strings = from sentence

Linq to Object をながめてみる var early. Bird. Query = string[] strings = from sentence in strings { let words = sentence. Split(' ', '. ') "A penny saved is a penny earned. ", from word in words "The early bird catches the worm. ", where !string. Is. Null. Or. Empty(word) "The pen is mightier than the sword. ", let w = word. To. Lower() "My name is M-now. " orderby w }; select w; foreach (var v in early. Bird. Query. Distinct()) Console. Write. Line(v); そんなに困らない わんくま同盟 東京勉強会 #17

Linq to Object をながめてみる double[] value = {1. 2, 3. 6, 2. 1, 10.

Linq to Object をながめてみる double[] value = {1. 2, 3. 6, 2. 1, 10. 5, 4. 8, 6. 3}; var calc. Query = from v in value orderby v select v; var cut. Query = calc. Query. Skip(1). Take(calc. Query. Count() - 2); Console. Write. Line("Count=" + cut. Query. Count()); Console. Write. Line("Sum=" + cut. Query. Sum()); Console. Write. Line("Average=" + cut. Query. Average()); Console. Write. Line("Max=" + cut. Query. Max()); Console. Write. Line("Mix=" + cut. Query. Min()); 困らないよ~~ わんくま同盟 東京勉強会 #17

Linq to Object をながめてみる Enumerableメソッド 機能 Concat 2つのIEnumerableの連結 Contains 要素がIEnumerableに格納されているかどうかを判断 Except 1つめのIEnumerableから2つめのIEnumerableの要素を削除 Intersect 2つのIEnumerableの積集合

Linq to Object をながめてみる Enumerableメソッド 機能 Concat 2つのIEnumerableの連結 Contains 要素がIEnumerableに格納されているかどうかを判断 Except 1つめのIEnumerableから2つめのIEnumerableの要素を削除 Intersect 2つのIEnumerableの積集合 Of. Type<Type> Typeで指定された型の物だけ抜き出す Range 指定範囲の整数のIEnumerableを作成 Repeat Reverse 一つの要素を繰り返し作成 IEnumerableの要素の順番を反転 Sequence. Equal 2つのIEnumerableが等しいか比較 Union 2つのIEnumerableの和集合 わんくま同盟 東京勉強会 #17

Linq to Object をながめてみる • For ループの置き換え – でも foreach でダンプしちゃつまんないけどね delegate T Y<T>(Y<T>

Linq to Object をながめてみる • For ループの置き換え – でも foreach でダンプしちゃつまんないけどね delegate T Y<T>(Y<T> y); Y<Func<Func<int, int>, Func<int, int>>> Y = y => f => x => f(y(y)(f))(x); Func<int, int>, Func<int, int>> g = f => x == 0 ? 1 : x * f(x - 1); var fact = from i in Enumerable. Range(1, 10) select Y(Y)(g)(i); foreach (var i in fact) Console. Write. Line(i); わんくま同盟 東京勉強会 #17

Linq to Object の正体 • どちらが簡単で分かりやすくてなおかつ速い のでしょうか? A) var accounts = from a in

Linq to Object の正体 • どちらが簡単で分かりやすくてなおかつ速い のでしょうか? A) var accounts = from a in al where a. Zip. Code == "168 -0064" select new { Name = a. Name, Zip. Code = a. Zip. Code };  foreach (var account in accounts) Console. Write. Line(account. Name + "(" + account. Zip. Code + ")"); B)  foreach (var a in al) if (a. Zip. Code == "168 -0064") Console. Write. Line(a. Name + "(" + a. Zip. Code + ")"); わんくま同盟 東京勉強会 #17

Linq to Object の正体 • どちらが速いでしょうか? A) static int[] cal 1(int[] arr) { int

Linq to Object の正体 • どちらが速いでしょうか? A) static int[] cal 1(int[] arr) { int sum = 0; int count = 0; int max = int. Min. Value; int min = int. Max. Value; foreach (int a in arr) { sum += a; count++; if (max < a) max = a; if (min > a) min = a; } return new int[] { sum, count, max, min }; } B) static int[] cal 2(int[] arr) { return new int[] { arr. Sum(), arr. Count(), arr. Max(), arr. Min() }; } わんくま同盟 東京勉強会 #17

Linq to SQL の使いどころ • たったこれだけのソースで、WPFのコンボボ ックスの選択可能なデータがセットできます。 using (NWData. Context context = new NWData.

Linq to SQL の使いどころ • たったこれだけのソースで、WPFのコンボボ ックスの選択可能なデータがセットできます。 using (NWData. Context context = new NWData. Context()) { var customer. Combo. Box. Query = from customer in context. Customers select new { ID = customer. Customer. ID, Name = customer. Company. Name }; this. combo. Box 1. Items. Source = customer. Combo. Box. Query; this. combo. Box 1. Selected. Value. Path = "ID"; this. combo. Box 1. Display. Member. Path = "Name"; } わんくま同盟 東京勉強会 #17

Linq to SQL の使いどころ • さらに速くする方法もあります – Compiled. Query. Compile var query = Compiled.

Linq to SQL の使いどころ • さらに速くする方法もあります – Compiled. Query. Compile var query = Compiled. Query. Compile( (Linq. Test. Data. Context db) => from a in db. Table_1 select a ); 使い方: foreach (var a in query(context)) – context. Object. Tracking. Enabled = false; • データの変更を追跡しない Query にみの場合に利用 できます。 わんくま同盟 東京勉強会 #17