トップページに戻る
次のC#のサンプルへ
前のC#のサンプルへ
6-4 Select拡張メソッド
C#のサンプル
メソッド構文での使用例
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
Console.WriteLine("■■■式形式■■■");
int[] ArrInt = { 1, 2, 3, 4 };
var query1 = ArrInt.Select(A => new
{
Col1 = A * 100,
Col2 = (A % 2 == 0).ToString(),
Col3 = (A % 2 == 1).ToString(),
});
foreach (var Any in query1) Console.WriteLine(Any);
Console.WriteLine("■■■文形式■■■");
IEnumerable<string> query2 = Enumerable.Range(1, 6).
Select(A =>
{
if (A % 3 == 0 && A % 5 == 0) return "Fizz Buzz";
if (A % 3 == 0) return "Fizz";
if (A % 5 == 0) return "Buzz";
return A.ToString();
});
foreach (string Any in query2) Console.WriteLine(Any);
Console.WriteLine("■■■連番を作成してくれる、select拡張メソッドのオーバーロード■■■");
IEnumerable<string> query3 = Enumerable.Repeat("連番", 3).Select((A, rn) => A + rn);
foreach (string Any in query3) Console.WriteLine(Any);
}
}
//出力結果
//■■■式形式■■■
//{ Col1 = 100, Col2 = False, Col3 = True }
//{ Col1 = 200, Col2 = True, Col3 = False }
//{ Col1 = 300, Col2 = False, Col3 = True }
//{ Col1 = 400, Col2 = True, Col3 = False }
//■■■文形式■■■
//1
//2
//Fizz
//4
//Buzz
//Fizz
//■■■連番を作成してくれる、select拡張メソッドのオーバーロード■■■
//連番0
//連番1
//連番2
クエリ構文での使用例
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
Console.WriteLine("■■■式形式■■■");
int[] ArrInt = { 1, 2, 3, 4 };
var query1 = from A in ArrInt
select (new
{
Col1 = A * 100,
Col2 = (A % 2 == 0).ToString(),
Col3 = (A % 2 == 1).ToString(),
});
foreach (var Any in query1) Console.WriteLine(Any);
Console.WriteLine("■■■文形式■■■");
Console.WriteLine("クエリ構文では、使えないようです。");
Console.WriteLine("■■■連番を作成してくれる、select拡張メソッドのオーバーロード■■■");
Console.WriteLine("クエリ構文では、使えないようです。");
}
}
//出力結果
//■■■式形式■■■
//{ Col1 = 100, Col2 = False, Col3 = True }
//{ Col1 = 200, Col2 = True, Col3 = False }
//{ Col1 = 300, Col2 = False, Col3 = True }
//{ Col1 = 400, Col2 = True, Col3 = False }
Arrayクラスでの代用案
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
internal class EntitiyClass
{
internal int Col1;
internal string Col2;
internal string Col3;
public override string ToString() //オーバーライド
{
return string.Format("Col1={0},Col2={1},Col3={2}", Col1, Col2, Col3);
}
}
static void Main()
{
Console.WriteLine("■■■式形式■■■");
int[] ArrInt = { 1, 2, 3, 4 };
EntitiyClass[] EntitiyArr = new EntitiyClass[ArrInt.Length];
for (int I = 0; I <= ArrInt.GetUpperBound(0); I++) {
EntitiyArr[I] = (new EntitiyClass()
{
Col1 = ArrInt[I] * 100,
Col2 = (ArrInt[I] % 2 == 0).ToString(),
Col3 = (ArrInt[I] % 2 == 1).ToString()
});
}
foreach (EntitiyClass Any in EntitiyArr) Console.WriteLine(Any);
Console.WriteLine("■■■文形式■■■");
int[] intQuery = Enumerable.Range(1, 6).ToArray();
string[] stringQuery = new string[intQuery.Length];
for (int I = 0; I <= intQuery.GetUpperBound(0); I++) {
int currVal = intQuery[I];
if (currVal % 3 == 0 && currVal % 5 == 0) stringQuery[I] = "Fizz Buzz";
else if (currVal % 3 == 0) stringQuery[I] = "Fizz";
else if (currVal % 5 == 0) stringQuery[I] = "Buzz";
else stringQuery[I] = currVal.ToString();
}
foreach (string Any in stringQuery) Console.WriteLine(Any);
Console.WriteLine("■■■連番を作成してくれる、select拡張メソッドのオーバーロード■■■");
string[] query3 = Enumerable.Repeat("連番", 3).ToArray();
string[] stringQuery3 = new string[query3.Length];
for (int I = 0; I <= stringQuery3.GetUpperBound(0); I++) {
stringQuery3[I] = query3[I] + (I + 1).ToString();
}
foreach (string Any in stringQuery3) Console.WriteLine(Any);
}
}
Listジェネリックでの代用案
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
internal class EntitiyClass
{
internal int Col1;
internal string Col2;
internal string Col3;
public override string ToString() //オーバーライド
{
return string.Format("Col1={0},Col2={1},Col3={2}", Col1, Col2, Col3);
}
}
static void Main()
{
Console.WriteLine("■■■式形式■■■");
var BaseList1 = new List<int>() { 1, 2, 3, 4 };
var ResultList1 = new List<EntitiyClass>();
for (int I = 0; I <= BaseList1.Count-1; I++) {
ResultList1.Add(new EntitiyClass()
{
Col1 = BaseList1[I] * 100,
Col2 = (BaseList1[I] % 2 == 0).ToString(),
Col3 = (BaseList1[I] % 2 == 1).ToString()
});
}
foreach (var Any in ResultList1) Console.WriteLine(Any);
Console.WriteLine("■■■文形式■■■");
var BaseList2 = Enumerable.Range(1, 6).ToList();
var ResultList2 = new List<string>();
foreach (int Any in BaseList2) {
if (Any % 3 == 0 && Any % 5 == 0) ResultList2.Add("Fizz Buzz");
else if (Any % 3 == 0) ResultList2.Add("Fizz");
else if (Any % 5 == 0) ResultList2.Add("Buzz");
else ResultList2.Add(Any.ToString());
}
foreach (var Any in ResultList2) Console.WriteLine(Any);
Console.WriteLine("■■■連番を作成してくれる、select拡張メソッドのオーバーロード■■■");
var BaseList3 = Enumerable.Repeat("連番", 3).ToList();
var ResultList3 = new List<string>();
int Counter = 0;
foreach (string Any in BaseList3)
ResultList3.Add(Any + (Counter++));
foreach (string Any in ResultList3) Console.WriteLine(Any);
}
}
Dictionaryジェネリックでの代用案
作成中
DataTableクラスでの代用案
作成中
解説
作成中