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(); //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; } } }
Lamda简单使用
猜你喜欢
转载自www.cnblogs.com/macT/p/11731034.html
今日推荐
周排行