LINQ-查询表达式基础
一、LINQ查询的数据源从应用程序的角度来看,原始源数据的特定类型和结构并不重要。?应用程序始终将源数据视为?IEnumerable<T>?或?IQueryable<T>?集合。? 例如在 LINQ to XML 中,源数据显示为? 其中,数组类型并为实现?IEnumerable<T>?或?IQueryable<T>?,为什么也可进行LINQ查询呢? 二、查询可能会执行三种操作
IEnumerable<int> highScoresQuery = from score in scores where score > 80 orderby score descending select score;
? IEnumerable<string> highScoresQuery2 = from score in scores where score > 80 orderby score descending select $"The score is {score}";
?
int highScoreCount = (from score in scores where score > 80 select score) .Count(); 在前面的示例中,请注意在调用? IEnumerable<int> highScoresQuery3 = from score in scores where score > 80 select score; int scoreCount = highScoresQuery3.Count(); 三、查询表达式查询表达式必须以?from?子句开头,且必须以?select?或?group?子句结尾。在第一个? 下面的代码示例演示一个简单查询表达式,它具有一个数据源、一个筛选子句、一个排序子句并且不转换源元素。?该查询以? ? static void Main() { // Data source. int[] scores = { 90,71,82,93,75,82 }; // Query Expression. IEnumerable<int> scoreQuery = //query variable from score in scores //required where score > 80 // optional orderby score descending // optional select score; //must end with select or group // Execute the query to produce the results foreach (int testScore in scoreQuery) { Console.WriteLine(testScore); } } // Outputs: 93 90 82 82 查询变量在上面的示例中, 开始查询表达式查询表达式必须以? IEnumerable<Country> countryAreaQuery = from country in countries where country.Area > 500000 //sq km select country; 范围变量一直处于范围中,直到查询使用分号或 continuation 子句退出。 查询表达式可能会包含多个? IEnumerable<City> cityQuery = from country in countries from city in country.Cities where city.Population > 10000 select city; 结束查询表达式查询表达式必须以? group 子句使用? ? var queryCountryGroups = from country in countries group country by country.Name[0]; select 子句使用? ? IEnumerable<Country> sortedQuery = from country in countries orderby country.Area select country;
// Here var is required because the query // produces an anonymous type. var queryNameAndPop = from country in countries select new { Name = country.Name,Pop = country.Population }; 使用“into”进行延续可以在? ? // percentileQuery is an IEnumerable<IGrouping<int,Country>> var percentileQuery = from country in countries let percentile = (int) country.Population / 10_000_000 group country by percentile into countryGroup where countryGroup.Key >= 20 orderby countryGroup.Key select countryGroup; // grouping is an IGrouping<int,Country> foreach (var grouping in percentileQuery) { Console.WriteLine(grouping.Key); foreach (var country in grouping) Console.WriteLine(country.Name + ":" + country.Population); } 筛选、排序和联接在开头? ? where 子句?使用? IEnumerable<City> queryCityPop = from city in cities where city.Population < 200000 && city.Population > 100000 select city; orderby 子句使用? ? IEnumerable<Country> querySortedCountries = from country in countries orderby country.Area,country.Population descending select country;
join 子句使用? ? var categoryQuery = from cat in categories join prod in products on cat equals prod.Category select new { Category = cat,Name = prod.Name }; 还可以通过使用?into?关键字将? let 子句?使用? string[] names = { "Svetlana Omelchenko","Claire O‘Donnell","Sven Mortensen","Cesar Garcia" }; IEnumerable<string> queryFirstNames = from name in names let firstName = name.Split(‘ ‘)[0] select firstName; foreach (string s in queryFirstNames) Console.Write(s + " "); //Output: Svetlana Claire Sven Cesar 查询表达式中的子查询查询子句本身可能包含查询表达式,这有时称为子查询。?每个子查询都以自己的? ? var queryGroupMax = from student in students group student by student.GradeLevel into studentGroup select new { Level = studentGroup.Key,HighestScore = (from student2 in studentGroup select student2.Scores.Average()) .Max() }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |