2016年7月7日木曜日

LINQ サンプル いろいろ

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;


namespace LINQ_Query
{
    class Fruit {
        public string code;
        public string name;
        public int price;

    }
    class Program
    {

        static string[] getString(string[] str) {
            return (from s in str where s.StartsWith("A") select s).ToArray();

        }

        static void Main(string[] args)
        {
            int[] numbers = { 1, 2, 3, 4, 5 };
            //クエリ式バージョン
            var query = from x in numbers
                        where x>= 2 && x <= 5
                        select x;
            foreach (var o in query) {
                Debug.WriteLine(o);

            }
          
            //ラムダ式バージョン
            var lamdaQuery = numbers.Where((x) => x >= 3 && x <= 4);
            foreach (var o in lamdaQuery)
            {
                Debug.WriteLine(o);//B

            }
            Debug.WriteLine("test");
            Console.ReadLine();


            string str = "ABC";
            var qSkip = str.Skip(1);
            foreach (var o in qSkip)
            {
                Debug.WriteLine(o);//BC
            }

            var qSkipTake = str.Skip(1).Take(1);
            foreach (var o in qSkipTake)
            {
                Debug.WriteLine(o);//BC
            }




            Debug.WriteLine("test");
            //Console.ReadLine();
              
         

            string[] fruit = { "Orange", "Apple", "Grape"};
            string[] result = getString(fruit);
            foreach (var name in result) Debug.WriteLine(name);


            int[] num = { 50, 200, 15, 43, 41, 333, };
            int qCount = num.Select(x=>x).Count();
            Debug.WriteLine(qCount);//6

            int qSum = num.Select(x => x).Sum();
            Debug.WriteLine(qSum);//682


            var r = from n in num
                    where n >= 100
                    orderby n
                    select n ;
            foreach (var n in r) Debug.WriteLine(n);
            Debug.WriteLine("test");
         

            var r2 = num.Where((n) => n >= 10).OrderBy((n) => n);
            foreach (var n in r2) Debug.WriteLine(n);


            Debug.WriteLine("test");
          
            var r3 = from n in num
                     select n * 100 / 30;
            foreach (var n in r3) Debug.WriteLine(n);

            Debug.WriteLine("test");
         

            var r4 = num.Select(n => n * 100 / 20);
            foreach (var n in r4) Debug.WriteLine(n);



            Debug.WriteLine("test");
     

            Fruit[] f = {
                new Fruit() { code="A100" , name ="Apple1", price=100},
                new Fruit() { code="A200" , name ="Apple2", price=200},
                new Fruit() { code="A300" , name ="Apple3", price=300},
                new Fruit() { code="A400" , name ="Apple4", price=400},

            };

            var q = from x in f
                    select x;
            foreach (var a in q) {
                Debug.WriteLine(a);
                Debug.WriteLine("code={0},name={1},price={2}", a.code, a.name, a.price);
                   
            };

//匿名オブジェクト
            var lamda_q = f.Select((n) => new { N=n.name,P=n.price});
            foreach (var a in lamda_q)
            {
                Console.WriteLine(a);
                Console.WriteLine("name={0},price={1}", a.N, a.P);

            };
            Debug.WriteLine("test");
            Console.ReadLine();

//匿名オブジェクト
            var q2 = from n in f
                     select new { N = n.name, P =n.price};

            foreach (var a in q2)
            {
                Debug.WriteLine(a);
                Debug.WriteLine("name={0},price={1}",  a.N, a.P);

            };


            Debug.WriteLine("test");
            //Console.ReadLine();

        }
    }
}
退避でわかりやすい
クエリ式とラムダ式サンプル多し

0 件のコメント:

コメントを投稿