lambda

什麼是lambda就不說了

首先建立一個實體對象並賦值

public class Person
    {
        public int PId { get; set; } //自增ID

        public string Name { get; set; }

        public int Age { get; set; }

      
    }

    List<Person> pA = new List<Person>();
            pA.Add(new Person() { PId = 1, Name = "張三", Age = 16, JobId = 1 });
            pA.Add(new Person() { PId = 2, Name = "小紅", Age = 18, JobId = 2 });
            pA.Add(new Person() { PId = 3, Name = "王武", Age = 20, JobId = 3 });
            pA.Add(new Person() { PId = 4, Name = "小梅", Age = 17, JobId = 4 });
            pA.Add(new Person() { PId = 5, Name = "小李", Age = 24, JobId = 3 }); 

    public class Job 
    {
        public int JobId { get; set; } //自增ID
        public string JobName { get; set; }
        public int WorkAge { get; set; }
    }

List<Job> jB = new List<Job>();
            jB.Add(new Job() { JobId = 1, JobName = "製造業", WorkAge = 3 });
            jB.Add(new Job() { JobId = 2, JobName = "IT行業", WorkAge = 5 });
            jB.Add(new Job() { JobId = 3, JobName = "建築業", WorkAge = 2 });
            jB.Add(new Job() { JobId = 4, JobName = "金融業", WorkAge = 1 }); 




var result=PA.select(p=>p).toList();     //查詢全部人(select查詢)
var result1=PA.Where(p=>p.Age>18).ToList();   //年齡大於18的person集合(where條件)
Person per = PA.SingleOrDefault(p => p.Age == 1);  //Age=1的單個people類
var netCheck = netSurveysList.FirstOrDefault(p => p.Id == netConfig.Id);  //返回序列中知足條件的第一個元素Person類

var result2=PA.Where(p=>p.Age>1 && p.JobId==3).ToList();  //條件 且
var result1=PA.Where(p=>p.Age>18 && p.JobId==3).ToList();
var persons = persons.Where(p => p.Name.Contains("兒子")).ToList();   //全部Name包含兒子的Person的集合

var persons = persons.Where(r => r.Name.StartsWith("s")).ToList();    //名字以s開始,t結束
var persons = persons.Where(r => r.Name.EndsWith("t")).ToList();

var num= persons.Max(r=>r.PId);  //最大值
var num= persons.Min(r=>r.PId);  //最小值
var num= persons.Average(r => r.PId);  //平均值
var num= persons.Sum(r => r.PId);  //求和
int[] array1={1,2,3};
int [] array2={2,4,6}
var quey1=array1.union(array2)  //並集 1,2,3,4,6
var quey2=array1.Intersect(array2)  //交集 2

var num= persons.OrderBy(r => r.PId).ToList(); //順序 var num= persons.OrderByDescending(r => r.PId).ToList(); //倒序


var result4 = pA.Where(k => k.Age > 10).GroupBy(j => j.JobId).Select(l => l.Key).ToList(); //單個字段 var result5 = pA.Where(k => k.Age > 10) .GroupBy(a => new Person{ PId=a.PId , Name=a.Name, Age=a.Age, JobId=a.JobId }) //多個字段 .Select(a => a.Key).ToList();


  var employee = empList.Select(item => new Student   //將序列中的每一個元素投影到新表單。返回List<Student>sql

  {
    ID = item.ID,
    FName = item.FName,
    Age = item.Age,
    Sex = item.Sex,
  }).ToList()spa

var result13 = pA.Select(k => k.JobId).Distinct();  //單列去重

var result9 = pA.Where(k => k.Age > 10).Count();   //count() (總行數)

var result10 = pA.Average(k => k.Age);  //avg() (平均年齡)

var result11 = pA.Where(k => k.Name.Contains("小")).ToList();   //Contains (至關於sql中like)檢索名稱中有小字    
注意:若是檢索的是英文的話,須要大小寫轉換,由於linq區分大小寫!
var result154 = pA.Where(o => o.Age > 18).Take(3).ToList(); //獲取前幾條 Take (至關於top) //兩個同一對象的list合併 personList.ForEach(a => personList2.Add(new person() { PId = a.pId, Name= a.Name, Age= a.Age}));
相關文章
相關標籤/搜索