Linq学习笔记(一):使用Where语句筛选数据
1、使用where单条件筛选
2、使用where双条件筛选
1 /// <summary>
2 /// 查询出大于5的记录
3 /// </summary>
4 private static void UseWhere()
5 {
6 int[] numbers = { -1, -2, -3, -4, -5, -6, -7, -8, -9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
7 var result = from n in numbers where n > 5 select n;
8 foreach (var n in result)
9 {
10 Console.WriteLine(n);
11 }
12 }
2 /// 查询出大于5的记录
3 /// </summary>
4 private static void UseWhere()
5 {
6 int[] numbers = { -1, -2, -3, -4, -5, -6, -7, -8, -9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
7 var result = from n in numbers where n > 5 select n;
8 foreach (var n in result)
9 {
10 Console.WriteLine(n);
11 }
12 }
2、使用where双条件筛选
1 private static void UseWhere()
2 {
3 int[] numbers = { -1, -2, -3, -4, -5, -6, -7, -8, -9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
4 var result = from n in numbers
5 where n < 0 && n > 5
6 select n;
7 }
2 {
3 int[] numbers = { -1, -2, -3, -4, -5, -6, -7, -8, -9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
4 var result = from n in numbers
5 where n < 0 && n > 5
6 select n;
7 }
3、使用Where带索引筛选
1 private static void UseWhere()
2 {
3 string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
4 var shortDigits = digits.Where((digit, index) => digit.Length < index);
5
6 foreach (var d in shortDigits)
7 {
8 Console.WriteLine(d);
9 }
10 }
2 {
3 string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
4 var shortDigits = digits.Where((digit, index) => digit.Length < index);
5
6 foreach (var d in shortDigits)
7 {
8 Console.WriteLine(d);
9 }
10 }