首页 > 代码库 > 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 日常关键字使用