首页 > 代码库 > linq 日常关键字使用
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 日常关键字使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。