C# Lambada表达式 总结

本文介绍了LINQ中的多种方法,包括Where、GroupBy、Count、Skip、Take等,并通过实例展示了如何利用这些方法进行数据筛选、分组统计及排序等操作。

 

1. Whereメソッド

using System.Linq;

class Program
{
    static void Main()
    {
        int[] work = { 1, 2, 3, 4, 5, 6 };
        var work2 = work.Where((x) => x > 3);
        foreach (var a in work2) {
            System.Console.WriteLine(a.ToString());
        }
    }
}

//出力結果
4
5
6
 
例子

        private void button2_Click(object sender, EventArgs e)         {             string[] strArray = { "one", "two", "three", };

            var query = from s in strArray                         let index = Array.IndexOf(strArray, s)                         where index % 2 > 0 && s.StartsWith("t")                         select s;             foreach (var q in query)             {                 MessageBox.Show(q);             }          

        }


2. GroupByメソッドとCountメソッド

using System.Linq;

class Program
{
    static void Main()
    {
        int[] tmp = { 1, 1, 2, 2, 3, 3, 3, 4, 5 };
        var work = tmp.GroupBy(X => X).Select(X => new { X.Key, cnt = X.Count()});
        foreach (var a in work) {
            System.Console.WriteLine(a);
        }
    }
}

//出力結果
{ Key = 1, cnt = 2 }
{ Key = 2, cnt = 2 }
{ Key = 3, cnt = 3 }
{ Key = 4, cnt = 1 }
{ Key = 5, cnt = 1 }


3. Skipメソッド,Takeメソッド

using System.Linq;

class Program
{
    static void Main()
    {
        int[] tmp1 = { 1, 1, 2, 2, 3, 4, 5 };

        var work = tmp1.OrderBy(X => X)
                       .Skip(2)
                       .Take(3);
        foreach (int a in work) {
            System.Console.WriteLine(a);
        }
    }
}

//出力結果
2
2
3


4. Unionメソッド,Concatメソッド

using System.Linq;

class Program
{
    static void Main()
    {
        int[] tmp1 = { 1, 1, 2, 2, 3, 4, 5 };
        int[] tmp2 = { 8, 7 };
        int[] tmp3 = { 6, 7 };

        //ConcatがSQLのUnion all
        //UnionがSQLのUnion
        var work = tmp1.Concat(tmp2)
                   .Concat(tmp3);
        foreach (var a in work) {
            System.Console.WriteLine(a);
        }
    }
}

//出力結果
1
1
2
2
3
4
5
8
7
6
7


5. FizzBuzz

using System.Linq;

class Program
{
    static void Main()
    {
        var work = Enumerable.Range(1, 100)
            .Select(X =>
            {
                if (X % 3 == 0 && X % 5 == 0) return "FizzBuzz";
                if (X % 3 == 0) return "Fizz";
                if (X % 5 == 0) return "Buzz";
                return X.ToString();
            });
        foreach (var a in work) {
            System.Console.WriteLine(a);
        }
    }
}

//出力結果
1
2
Fizz
4
Buzz
以下略


6. Reverse,OrderBy,OrderByDescendingメソッド

using System.Linq;

class Program
{
    static void Main()
    {
        int[] arr = { 2, 4, 8, 3, 5, 7 };
        //arr = arr.Reverse().ToArray();
        arr = arr.OrderBy(X => X).ToArray();
        //arr = arr.OrderByDescending(X => X).ToArray();

        foreach (int each in arr) {
            System.Console.WriteLine(each);
        }
    }
}


7. 匿名classの配列

using System.Linq;

class Program
{
    static void Main()
    {
        var arr = new[] {new{ID = 1,Val = 3},
                         new{ID = 1,Val = 4},
                         new{ID = 1,Val = 5},
                         new{ID = 2,Val = 6},
                         new{ID = 2,Val = 7},
                         new{ID = 2,Val = 8},};
        var SQL = arr.GroupBy(X => X.ID)
                     .Select(X => new { X.Key, cnt = X.Count() });

        foreach (var each in SQL) {
            System.Console.WriteLine(each);
        }
    }
}


8. 組み合わせの列挙

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string line = "1,2 5,6,7";
        var with1 = line.Split(' ').Select(c => c.Split(',').AsEnumerable());
        var query = with1.Aggregate((xs, ys) => xs.SelectMany(_ => ys, (x, y) => x + '-' + y));
        foreach (var each in query) Console.WriteLine(each);
    }
}
//出力結果
//1-5
//1-6
//1-7
//2-5
//2-6
//2-7


9. 複数項目をSum

using System.Linq;

class Program
{
    static void Main()
    {
        var arr = new[] {new{Val1 = 1,Val2 = 3},
                         new{Val1 = 0,Val2 = 7},
                         new{Val1 = 4,Val2 = 5},
                         new{Val1 = 0,Val2 = 5},
                         new{Val1 = 5,Val2 = 2},
                         new{Val1 = 5,Val2 = 8},};

        var query = arr.GroupBy(A => 1)
                       .Select(A => new
                       {
                           sumVal1 = A.Sum(B => B.Val1),
                           sumVal2 = A.Sum(B => B.Val2),
                           cnt = A.Count()
                       });
        foreach (var each in query) {
            System.Console.WriteLine(each);
        }
    }
}
//出力結果
//{ sumVal1 = 15, sumVal2 = 30, cnt = 6 }


10. Select拡張メソッドでRowNum擬似列もどき

MSDN --- Select(TSource, TResult) メソッド

using System.Linq;

class Program
{
    static void Main()
    {
        var arr = new[] {new{Val1 = 1,Val2 = 3},
                         new{Val1 = 0,Val2 = 7},
                         new{Val1 = 4,Val2 = 5},
                         new{Val1 = 0,Val2 = 5},
                         new{Val1 = 5,Val2 = 2},
                         new{Val1 = 5,Val2 = 8},};
        var query = arr.Select((X, rn) => new { X.Val1, X.Val2, rn });
        foreach (var each in query) {
            System.Console.WriteLine(each);
        }
    }
}
//出力結果
//{ Val1 = 1, Val2 = 3, rn = 0 }
//{ Val1 = 0, Val2 = 7, rn = 1 }
//{ Val1 = 4, Val2 = 5, rn = 2 }
//{ Val1 = 0, Val2 = 5, rn = 3 }
//{ Val1 = 5, Val2 = 2, rn = 4 }
//{ Val1 = 5, Val2 = 8, rn = 5 }


次のネタ

・Any,All
・TakeとTakeWhile
・SkipとSkipWhile
・OrderBy,ThenBy
・OrderByDescending,ThenByDescending
・GroupBy
・Count,Sum,Max,Min,Average
・Join,GroupJoin
・SelectMany

・case式

・intersect
・intersect all
・except
・except all

・再帰SQL

・Bool_And関数
・Bool_Or関数

・クロスジョイン
・等価結合
・内部結合
・左外部結合
・完全外部結合
・Partitioned Outer Join

・Pivot
・UnPivot

・stats_mode関数
・median関数

・max関数
・count関数
・ListAgg関数
・Row_Number関数
・dense_rank関数
・rank関数
・Lag関数
・Lead関数
・First_Value関数
・Last_Value関数

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值