LINQ标准查询运算符
标准查询运算符:
标准查询运算符是一组方法,提供包括筛选where、投影select、聚合(例如max)、排序order by等在内的查询功能。
string sentence = "the quick brown fox jumps over the lazy dog"; string[] words = sentence.Split(' '); var query = from word in words group word.ToUpper() by word.Length into gr orderby gr.Key select new { Length = gr.Key, Words = gr }; //与上面表达同一种意思的方法调用写法 var query1 = words.GroupBy(x => x.Length, w => w.ToUpper()) .OrderBy(o => o.Key) .Select(p => new { Length = p.Key, Words = p });
各个标准查询运算符在执行时间上有所不同, 返回单一值的方法例如Max、Sum会立即执行,返回值序列的延迟执行。
运算符对应的查询表达式:
Cast方法 public static System.Collections.Generic.IEnumerable<TResult> Cast<TResult> (this System.Collections.IEnumerable source);
例如 from string fruit in fruits
System.Collections.ArrayList fruits = new System.Collections.ArrayList(); fruits.Add("mango"); fruits.Add("apple"); fruits.Add("lemon");
//查询表达式 var query = from string fruit in fruits orderby fruit select fruit;
//方法调用
IEnumerable<string> query1 =
fruits.Cast<string>().OrderBy(fruit => fruit).Select(fruit => fruit);
GroupBy方法 group ... by ... 或 group ... by ... into ...
GroupJoin分组联接方法 join ... in ... on ... equals ... into ...
namespace ConsoleApp4 { class Program { static void Main(string[] args) { Person magnus = new Person { Name = "Hedlund, Magnus" }; Person terry = new Person { Name = "Adams, Terry" }; Person charlotte = new Person { Name = "Weiss, Charlotte" }; Pet barley = new Pet { Name = "Barley", Owner = terry }; Pet boots = new Pet { Name = "Boots", Owner = terry }; Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte }; Pet daisy = new Pet { Name = "Daisy", Owner = magnus }; List<Person> people = new List<Person> { magnus, terry, charlotte }; List<Pet> pets = new List<Pet> { barley, boots, whiskers, daisy }; //join … in … on … equals … into … 查询表达式 var query = from person in people join pet in pets on person equals pet.Owner into petGroup select new { OwnerName = person.Name, Pets = petGroup.Select(p=>p.Name) }; //方法调用 var query1 = people.GroupJoin(pets, person => person, pet => pet.Owner, (person, petCollection) => new { OwnerName = person.Name, Pets = petCollection.Select(pet => pet.Name) } ); Console.WriteLine(" 查询表达式 :"); foreach (var obj in query) { // Output the owner's name. Console.WriteLine("{0}:", obj.OwnerName); // Output each of the owner's pet's names. foreach (string pet in obj.Pets) { Console.WriteLine(" {0}", pet); } } Console.WriteLine(" 方法调用 :"); foreach (var obj in query1) { // Output the owner's name. Console.WriteLine("{0}:", obj.OwnerName); // Output each of the owner's pet's names. foreach (string pet in obj.Pets) { Console.WriteLine(" {0}", pet); } } } } class Person { public string Name { get; set; } } class Pet { public string Name { get; set; } public Person Owner { get; set; } } }
Join方法 join...in...on...equals...
SelectMany 方法将序列的每个元素投影到 IEnumerable<T> 并将结果序列合并为一个序列。
SelectMany方法对应复合from查询表达式
namespace ConsoleApp4 { class Program { static void Main(string[] args) { PetOwner[] petOwners = { new PetOwner { Name="Higa", Pets = new List<string>{ "Scruffy", "Sam" } }, new PetOwner { Name="Ashkenazi", Pets = new List<string>{ "Walker", "Sugar" } }, new PetOwner { Name="Price", Pets = new List<string>{ "Scratches", "Diesel" } }, new PetOwner { Name="Hines", Pets = new List<string>{ "Dusty" } } }; var query1 = from owner in petOwners from pet in owner.Pets select new { petOwner = owner.Name, petName = pet }; foreach (var obj in query1) { Console.WriteLine(obj); } Console.WriteLine("方法调用结果:"); var query = petOwners .SelectMany(petOwner => petOwner.Pets, (petOwner, petName) => new { petOwner.Name, petName }) ; // Print the results. foreach (var obj in query) { Console.WriteLine(obj); } } } class PetOwner { public string Name { get; set; } public List<string> Pets { get; set; } } }
OrderBy 、OrderByDescending、ThenByDescending 、ThenBy等排序方法对应表达式orderby...
descending/ascending(默认)。
Where方法对应where....
量变会引起质变。