美文网首页
LINQ入门示例

LINQ入门示例

作者: 心彻 | 来源:发表于2024-08-04 17:46 被阅读0次

    入门示例:

    //demo1
    // even ,>=4
    // then sort
    var list = new List<int> {1,3,5,7,9,2,4,6,8,0};
    //1.Imperative programmming
    var res1=new List<int>();
    foreach(var a in list)
    {
        if(a%2==0&&a>=4)
        {
            res1.Add(a);
        }
    }
    res1.Sort();
    res1.Dump();
    
    //2.query expression
    var res2=
        from a in list
        where a%2==0 && a>=4
        orderby a
        select a;
    
    res2.Dump();    
    
    //3. chained expression
    var res3=list.Where(a=>a%2==0 &&a>=4).OrderBy(a=>a);
    res3.Dump();
    
    
    
    //demo2 求交集
    var arr1 = new int[] { 1, 2, 3, 4, 5, 6 };
    var arr2 = new int[] { 4, 5, 6, 7, 8, 9 };
    
    //1.Imperative programming
    var res1 = new List<int>();
    foreach (var a in arr1)
    {
        if (arr2.Contains(a))
        {
            res1.Add(a);
        }
    }
    res1.Dump();
    
    //2. query expression
    var res2=
        from a in arr1
        where arr2.Contains(a)
        select a;
    res2.Dump();
    
    //3. chained expression
    var res3=arr1.Intersect(arr2);
    res3.Dump();
    
    //demo3 分组
    var rnd=new Random(1334);
    var arr=Enumerable.Range(0,200).Select(_=>rnd.Next(20));
    
    //1. Imperative Programming
    var dic=new Dictionary<int,int>();
    foreach(var n in arr)
    {
        if(dic.TryGetValue(n,out int count))
            dic[n]=count+1;
        else
            dic[n]=1;
    }
    dic.Dump();
    
    //2. query expression
    var res2=
        from a in arr
        group a by a into b
        orderby b.Key
        select new {b.Key,Count=b.Count()};
    res2.Dump();    
        
        
    //3. chained expression
    var res3=
        arr.GroupBy(a=>a).OrderBy(b=>b.Key) 
        .Select(b=>new{b.Key,Count=b.Count()});
    res3.Dump();
    
    //demo4 ParallelQuery
    var arr=Enumerable
        .Range(1,10)
        .ToArray()
        .AsParallel()
        .AsOrdered()
        .Select(x => 
        {
            Thread.Sleep(500);
            return x*x;
        })
        .AsSequential();
    arr.Dump();
    

    实战示例:

    //展平
    var mat = new int[][] {
        new[]{1,2,3,4},
        new[]{5,6,7,8},
        new[]{9,10,11,12}
    };
    
    //1.query expression
    var res1=
        from row in mat
        from n in row
        select n;
    res1.Dump();
    
    //2.chained expression
    var res2=
        mat.SelectMany(n=>n);
    res2.Dump();
        
    
    //笛卡儿积
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<4;j++)
        {
            for(int k=0;k<3;k++)
            {
                $"{i},{j},{k}".Dump();
            }
        }
    }
    
    //query expression
    var res1=
        from i in Enumerable.Range(0,5)
        from j in Enumerable.Range(0,4)
        from k in Enumerable.Range(0, 3)
        select $"{i},{j},{k}";
    res.Dump(); 
    
    //chained expression 
    var res2=
        Enumerable.Range(0,5)
        .SelectMany(r => Enumerable.Range(0, 4), (l, r) => (l, r))
        .SelectMany(r => Enumerable.Range(0, 3),(l, r) => (l.l,l.r,r))
        .Select(x=>x.ToString());
    res2.Dump();    
    
    

    相关文章

      网友评论

          本文标题:LINQ入门示例

          本文链接:https://www.haomeiwen.com/subject/maitkjtx.html