一 什么是LINQ?
LINQ即Language Integrated Query(语言集成查询),LINQ是集成到C#和Visual Basic.NET这些语言中用于提供查询数据能力的一个新特性。
二 简单的Linq例子。(简单形式我就不介绍了,主要以Lambda形式介绍)。
///针对于DataTable ///Linq表达式 var drArry = from r in dtZuheData.AsEnumerable() where r["SECURITIESCODE"].ToString().Trim() == "100" group r by new { Date = r.Field<decimal>("datetime").ToString().Substring(0, 6), name = r.Field<string>("NAME") } into g orderby g.Key.Date descending select new { Date = g.Key.Date, Name = g.Key.name, MaxPrice = g.Max(p => p.Field<decimal>("datetime")) }; ///等同的Lamda表达式 var listMaxDateDay = dtZuheData.AsEnumerable().Where(r => r["SECURITIESCODE"].ToString().Trim() == "100")
.GroupBy(r => new { Date = r["datetime"].ToString().Substring(0, 6) })
.Select(g => (new { name = g.Key.Date, Date = g.Max(t => t.Field<decimal>("datetime")) }))
.OrderBy(a => a.Date);
Linq和Lamda表示中的几种对比表达形式
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(); //wherevar result2 = pA.Where(k => k.Age > 18 && k.JobId==3 ).ToList(); // andvar 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();