第133回 LINQとラムダ式 その1

公開日:2015-02-05 更新日:2019-05-11

1. 概要

独り言によるプログラミング講座、「第133回 LINQとラムダ式 その1」です。
LINQを使って、配列やリストからデータの抽出、加工を行う方法について説明しています。

2. 動画



3. 動画中に書いたソース

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //★独り言によるプログラミング講座

            //■LINQとラムダ式について
            
            //LINQ
            //  配列やリスト -> 値の抽出、加工

            //ラムダ式
            //  引数 => 戻り値
            //  引数 => { 処理; 処理; 処理; return 戻り値; }

            //string s = "123,456,789";

            /*
            int count = 0; //カンマの数
            for (int i = 0; i < s.Length; i++) {
                //1文字ずつチェック
                if (s[i] == ',') {
                    count++;
                }
            }
            */
            
            //int count = s.Count(_ => _ == ',');

            //int count = s.Where(_ => _ == ',').Count();

            //MessageBox.Show(count.ToString());

            /*
            //遅延評価。まだ実行されない。式だけ定義している状態。
            var list = s.Where(_ => _ == ',');
            foreach(var item in list) {
                MessageBox.Show(item.ToString());
            }
            */

            
            //int[] array = {1, 2, 3, 4, 5};
            /*
            foreach(var item in array.Where(_ => _ > 3) ) {
                MessageBox.Show(item.ToString());
            }
            */
            /*
            foreach(var item in array.Select(_ => _ * 2)) {
                MessageBox.Show(item.ToString());
            }
            */

            /*
            int count = 
                array.Where(_ => _ >= 3).Select(_ => _ * 2).Count();
            MessageBox.Show(count.ToString());
            */

            /*
            for (int i = 0; i < array.Length; i++) {
                MessageBox.Show(array[i].ToString());
            }
            */
            /*
            foreach (var x in array) {
                MessageBox.Show(x.ToString());
            }
            */
            /*
            int count = array.Count(x => {
                MessageBox.Show(x.ToString());

                return true;
            });
            MessageBox.Show(count.ToString());
            */
            /*
            array.Select(x => {
                MessageBox.Show(x.ToString());
                return x;
            }).Count();
            */
            /*
            array.Where(x => {
                MessageBox.Show(x.ToString());
                return true;
            }).Count();
            */

            /*
            Array.ForEach(array, (x => {
                MessageBox.Show(x.ToString());
            }));

            array.Count(x => {
                MessageBox.Show(x.ToString());
                return true;
            });
             * 
            //思考の順番
            // 1 + 2 * 3 よりも、
            // 2 * 3 + 1 の方がわかりやすい
            */

            /*
            //1,2,3,4,5 -> 3, 4
            array.Skip(2).Take(2).Select(_ => _ * 2).Count( _ => {
                MessageBox.Show(_.ToString());
                return true;
            });
            */
        }
    }
}