zoukankan      html  css  js  c++  java
  • Lamda简单使用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Lamda
    {
        class Program
        {
            static void Main(string[] args)
            {
                #region 测试数据
                List<Student> list = new List<Student>()
            {
            new Student(){ ID=1, Name="jack", Age=20},
            new Student(){ ID=2, Name="mary", Age=21},
            new Student(){ ID=3, Name="joe", Age=22},
             new Student(){ ID=4, Name="joe", Age=22},
            new Student(){ ID=5, Name="Aaron", Age=23},
              new Student(){ ID=6, Name="Aaron", Age=24},
            };
                var a = list.Where((x, i) => list.FindIndex(z => z.ID == x.ID) == i);
                #endregion
                //1、获取list实体某一字段最大值
                var maxvalue = list.Max(p => p.ID);//4
                //2、获取list实体某一字段最小值
                var minvalue = list.Min(p => p.ID);//1
                //3、获取list实体某一字段总和的平均值
                var sumvalue = list.Sum(p => p.ID);//10
                //4、获取list实体某一字段总和的平均值
                var village = list.Average(p => p.ID);//2.5
                //5、判断list实体字段是否包含值
                var Iscon = list.Where(p => p.Name == "jack" && p.Age == 22).Any();//false  //是否存在名字为jack,年龄在2岁的人
                //6、list实体转成DICTIONARY 键值对
                var dic = list.Distinct().ToDictionary(p => p.ID, p => p.Name);
                //7、按照某一字段进行分组并获取对应字段的值
                var group = list.GroupBy(p => p.Name).Select(p =>p.Key).ToList();
    //7.1根据某一个字段的值去重
    var group2 =
    list.GroupBy(p => p.Name).Select(p =>p.First()).ToList();
               //8、根据某一字段获取list实体中重复的数据
    var duplicatevalue = list.GroupBy(p => p.Name).Where(g => g.Count() > 1).Select(p => p.Key).ToList();
    //已知Name字段值中有重复值,根据Name字段查出重复值
    var sbarry = string.Join("','", duplicatevalue.ToArray());
    var sdk = list.Where(p => sbarry.Contains(p.Name)).ToList();//找出符合条件的值重复的数据
    //9、根据某一字段获取list实体中重复的数据
    var data2 = list.Where(p => list.Count(x => x.Name == p.Name) > 1).ToList();//找出符合条件的值重复的数据(这种方法比上8的好处是直接就可以查出实体)
    int number = 5 - 1;
    var data = list.Where(p => list.Count(x => x.Name == p.Name) >= number).ToList();//统计重复数据是5条以上的
    //10、只获取list实体部分字段的值
    var PartList = list.Select(p => new { p.Name, p.ID}).ToList();
    var PartList2 = list.Where(p => p.ID==1).Select(p => new { p.Name, p.ID }).ToList();
    var r = PartList2.Select(p => p.ID).FirstOrDefault(); foreach (var item in PartList2) { }
    //11、只获取list实体部分字段的值
    var PartList3 = list.Select(p => new Student2 { Name = p.Name, ID = p.ID }).ToList();
    }
    }

    public class Student2 { public int ID { get; set; } public string Name { get; set; } } public class Student { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } } }
    //根据某一字段值获取最大值
                List<testLambdaMax> lmax = new List<testLambdaMax>{
                new testLambdaMax(){Id=1,Name="A"},
                new testLambdaMax(){Id=2,Name="B"},
                new testLambdaMax(){Id=3,Name="C"},
                new testLambdaMax(){Id=4,Name="D"}
            };
         
                var b = lmax.OrderByDescending(t => t.Id).Select(p => p.Name).FirstOrDefault();


    //根据list实体中某一字段值去重
    List<Test> t = new List<Test>() {
    new Test(){id=1,name="车辆"},
    new Test(){id=2,name="车辆"},
    new Test(){id=3,name="飞机"},
    new Test(){id=4,name="火车"},
    new Test(){id=4,name="火车"},
    };
    var q = t.Where(d1 => t.Count(d2 => d2.name == d1.name) > 1).Distinct().ToList();
  • 相关阅读:
    大规模web服务读书笔记 狼
    MVC3如果虚拟目录中有点号,会导致静态文件404 狼
    CDN服务商和CDN常见问题 狼
    中文字段名,问题根源查询无聊话题。 狼
    NET下Session共享的几种实现方式 狼
    企业应用架构读书笔记与总结 狼
    Redis简单本机测试 狼
    你是否经历过这些,求如何继续才能提升 狼
    WinDbg配置和使用基础
    Python IDLE入门
  • 原文地址:https://www.cnblogs.com/macT/p/11731034.html
Copyright © 2011-2022 走看看