Linq之Lambda表达式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linq之Lambda表达式相关的知识,希望对你有一定的参考价值。
一 什么是LINQ?
LINQ即Language Integrated Query(语言集成查询),LINQ是集成到C#和Visual Basic.NET这些语言中用于提供查询数据能力的一个新特性。 |
二 简单的Linq例子。(简单形式我就不介绍了,主要以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();
以上是关于Linq之Lambda表达式的主要内容,如果未能解决你的问题,请参考以下文章
点标记(lambda表达式+linq查询标记符)与linq语句(查询表达式)