linq 日常关键字使用
Posted Steven.Li
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linq 日常关键字使用相关的知识,希望对你有一定的参考价值。
1.from
var scoreQuery = from student in students from score in student.Scores where score > 90 select new { Last = student.LastName, score };
2.where
var queryLowNums3 = from num in numbers where num < 5 where num % 2 == 0 select num;
3.select
var studentQuery7 = from student in app.students where student.ID > 111 select new { student.First, student.Last };
4.group
var studentQuery = from student in students let avg = (int)student.Scores.Average() group student by (avg == 0 ? 0 : avg / 10) into g orderby g.Key select g;
5.into
var wordGroups1 = from w in words group w by w[0] into fruitGroup where fruitGroup.Count() >= 2 select new { FirstLetter = fruitGroup.Key, Words = fruitGroup.Count() };
6.orderby
var sortedGroups = from student in students orderby student.Last, student.First group student by student.Last[0] into newGroup orderby newGroup.Key select newGroup;
7.join
var innerJoinQuery = from category in categories join prod in products on category.ID equals prod.CategoryID select new { ProductName = prod.Name, Category = category.Name }; //produces flat sequence
var innerGroupJoinQuery = from category in categories join prod in products on category.ID equals prod.CategoryID into prodGroup select new { CategoryName = category.Name, Products = prodGroup };
8.ascending
IEnumerable<string> sortAscendingQuery = from vegetable in vegetables orderby vegetable ascending select vegetable;
9.descending
IEnumerable<string> sortDescendingQuery = from vegetable in vegetables orderby vegetable descending select vegetable;
10.on
var innerJoinQuery = from category in categories join prod in products on category.ID equals prod.CategoryID select new { ProductName = prod.Name, Category = category.Name };
11.equals
var innerJoinQuery = from category in categories join prod in products on category.ID equals prod.CategoryID select new { ProductName = prod.Name, Category = category.Name };
12.by
var query = from student in students group student by student.LastName[0];
以上是关于linq 日常关键字使用的主要内容,如果未能解决你的问题,请参考以下文章
等效于链式 LINQ 扩展方法调用中的 'let' 关键字的代码
等效于链式 LINQ 扩展方法调用中的 'let' 关键字的代码