Lambda表达式

一 什么是Lambda?

“Lambda 表达式”(lambda expression)是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的lambda抽象(lambda abstraction),是一个匿名函数,即没有函数名的函数。Lambda表达式可以表示闭包(注意和数学传统意义上的不同)。

二 简单的Lambda例子。

 1  首先创建一个实体对象类并赋值。

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

        public string Name { get; set; }

        public int Age { get; set; }

        public int JobId { 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 }); 

2 查询所有人:

var result = pA.Select(k => k).ToList();

3.where条件(年龄大于18的):

var result1 = pA.Where(k => k.Age > 18).ToList(); //where
var result2 = pA.Where(k => k.Age > 18 && k.JobId==3 ).ToList(); // and
var result3 = pA.Where(k => k.Age > 18 || k.JobId == 3).ToList(); // or

4.group by(一个字段和多个字段)

//单个字段
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();

4.1 Distinct(单列去重)

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

5.order by(排序,动态排序)

 var result6 = pA.Where(k => k.Age > 10)
                 .OrderBy(k => k.Age).ToList(); // asc

 var result7 = pA.Where(k => k.Age > 10)
                 .OrderByDescending(k => k.Age).ToList(); // desc
//先按年龄倒序,在按职业正序,在按名称倒序。
var result8 = pA.Where(k => k.Age > 10)
                .OrderByDescending(k => k.Age).ThenBy(k => k.JobId).ThenByDescending(k => k.Name).ToList();

//动态排序
var result81 = pA.OrderBy(k => GetPropertyValue(k, "Age")).ToList(); var result82 = pA.OrderBy(k => GetPropertyValue(k, "JobId")).ToList(); private static object GetPropertyValue(object obj, string property) { System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(property); return propertyInfo.GetValue(obj, null); }

 6 count() (总行数)

var result9 = pA.Where(k => k.Age > 10).Count();

7 avg() (平均年龄)

var result10 = pA.Average(k => k.Age);

8 Contains (相当于sql中like)检索名称中有小字:

注意:如果检索的是英文的话,需要大小写转换,因为linq区分大小写!

var result11 = pA.Where(k => k.Name.Contains("")).ToList();

9 join 

var result12 = pA.Join(jB, j => j.JobId, k => k.JobId, (j, k) => new {j.PId,j.Name,j.Age,k.JobName }).ToList();

10 left join (DefaultIfEmpty)

var result13 = pA.Join(jB, j => j.JobId, k => k.JobId, (j, k) => new { j.PId, j.Name, j.Age, k.JobName }).DefaultIfEmpty().ToList();

 11 Take (相当于top)

//获取前几条(相当于top)
var result154 = pA.Where(o => o.Age > 18).Take(3).ToList();

 

  转载请注明出处:http://www.cnblogs.com/bkyan/

 

posted @ 2016-06-02 10:05  い哎哟喂〤  阅读(2991)  评论(1编辑  收藏  举报