What is LINQ?
?
LINQ即Language Integrated Query(语言集成查询),LINQ是集成到C#和Visual Basic.NET这些语言中用于提供查询数据能力的一个新特性。 |
Two simple examples of LINQ. (In simple Form I will not introduce, mainly in the form of Lambda introduction).
1 first create an Entity object class and assign a value.
public class Person {public int PId {get; set;}//self-increment 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 = ' Zhang San ', age = +, JobId = 1}); Pa.add (new person () {PId = 2, Name = "Little Red", age = JobId = 2}); Pa.add (new person () {PId = 3, Name = ' Wang Wu ', age =, JobId = 3}); Pa.add (new person () {PId = 4, Name = "Mei", age = N, JobId = 4}); Pa.add (new person () {PId = 5, Name = "Xiao Li", age = JobId = 3}); public class Job {public int JobId {get; set;}//self-increment ID public string JobName {get; set;} public int Workage {get; set;} }list<job> JB = new list<job> (); Jb.add (New Job () {JobId = 1, JobName = "manufacturing", Workage = 3}); Jb.add (New Job () {JobId = 2, JobName = "IT Industry", Workage = 5}); JB.add (New Job () {JobId = 3, JobName = "Construction Industry", Workage = 2}); Jb.add (New Job () {JobId = 4, JobName = "Finance", Workage = 1});
2 Query everyone:
var result = Pa.select (k = k). ToList ();
3.where conditions (older than 18):
var result1 = pa.where (k = k.age > 18). ToList (); Wherevar result2 = pa.where (k = k.age > && k.jobid==3). ToList (); Andvar RESULT3 = pa.where (k = k.age > | | k.jobid = 3). ToList (); Or
4.group by (one field and multiple fields)
Single field var result4 = pa.where (k = k.age > 10). GroupBy (j = j.jobid). Select (L = l.key). ToList ();//multiple fields var result5 = pa.where (k = k.age >) . GroupBy (a = new person{pid=a.pid, name=a.name, Age=a.age, jobid=a.jobid}) . Select (A = A.key). ToList ();
4.1 Distinct (single-row to Heavy)
Single-Column de-result13 var = pa.select (k = k.jobid). Distinct ();
5.order by (sort, dynamic sort)
var result6 = pa.where (k = k.age >) . (k = k.age). ToList (); ASC var result7 = pa.where (k = k.age >) . OrderByDescending (k = k.age). ToList (); desc//first by age in reverse, in accordance with the occupational sequence, in reverse by name. var result8 = pa.where (k = k.age >) . OrderByDescending (k = k.age). ThenBy (k = k.jobid). ThenByDescending (k = k.name). ToList ();
Dynamic sort var result81 = Pa.orderby (k = GetPropertyValue (k, "age")). ToList (); var result82 = Pa.orderby (k = GetPropertyValue (k, "JobId")). ToList ();p rivate static object GetPropertyValue (Object obj, string property) { System.Reflection.PropertyInfo PropertyInfo = obj. GetType (). GetProperty (property);
6 count () (Total rows)
var result9 = pa.where (k = k.age > 10). Count ();
7 avg () (average age)
var result10 = pa.average (k = k.age);
8 Contains (equivalent in SQL) retrieves a small print in the name:
Note: If you are retrieving English, case conversion is required because LINQ is case-sensitive!
var result11 = pa.where (k = k.name.contains ("small")). 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 ();
Ten 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 ();
One take (equivalent to top)
Get the first few (equivalent top) var result154 = pa.where (o = o.age > 18). Take (3). ToList ();
The lambda Expression of LINQ