一 什麼是LINQ? 二 簡單的Linq例子。(簡單形式我就不介紹了,主要以Lambda形式介紹)。 1 首先創建一個實體對象類並賦值。 public class Person { public int PId { get; set; } // 自增Id public string Name { g ...
一 什麼是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();
轉載請註明出處:http://www.cnblogs.com/bkyan/