C# 中Linq的学习(查询表达式基础知识)示例
using System;
i:D
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinqStudy
x}N?1j988{
//***********************************************************************************************************
fm C s
//Module:Program.cs
//Author:limeteor
//Create Date:
//***********************************************************************************************************
class Program
{
static void Main(string[] args)
{
LinqQuery linqQuery = new LinqQuery();
linqQuery.OrderbyScoresQuery();
linqQuery.HighScoresQuery();
linqQuery.ScoresQueryCount();
Console.ReadKey();
}
}
k|J i\
//***********************************************************************************************************
//Module:LinqOrderby.cs
//Author: limeteor
//Create Date:
//***********************************************************************************************************
J B
class LinqQuery
{
/// <summary>
/// 返回值为void时的写法。排序(降序)
D#Aw3]S5[
R
/// </summary>
/// <returns></returns>
WyZJ!W
public int? OrderbyScoresQuery()
{
int[] scores = new int[] { 88, 79, 61, 59, 74, 58, 49, 97, 99, 84 };
if (scores.Length != 0)
{
//var scoresQuery = from score in scores where score > 70 orderby score descending select score;//查询语法
var scoresQuery = scores.Where(score => score > 70).OrderByDescending(score => score);//Lambda表达式(方法语法)
Console.WriteLine("下面的数据按降序排列");
foreach (var i in scoresQuery)
{
BHw+a
Console.WriteLine(i);
``| N‑n988 }
Console.WriteLine("数据输出已完成!");
V"Q988 return 0;
\ Kek:[8P6x j
Yl
n(]988 }
else
{
return null;
}
}
/// <summary>
H x'gBIQ2j R8D988 /// 从int 到 string 的转换
E1J5KW
/// </summary>
}&W2nS988 /// <returns></returns>
public int? HighScoresQuery()
{
X.X!V;LT6IM988 int[] scores = new int[] { 88, 79, 61, 59, 74, 58, 49, 97, 99, 84 };
if (scores.Length != 0)
{
];t)X988 //var scoresQuery = from score in scores where score > 70 orderby score descending select string.Format("The Score is {0}", score);//查询语法
var scoresQuery = scores.Where(score => score > 70).OrderByDescending(score => score);//Lambda表达式(方法语法)
Console.WriteLine("下面的数据是从int 到 string 的转换");
foreach (var i in scoresQuery)
{
Console.WriteLine(i);
}
Mbo,] M4|
Console.WriteLine("数据输出已完成!");
return 0;
}
cP N|7biuq
else
{
return null;
}
}
/// <summary>
/// 下面的查询从 scores 整数数组中返回高于 70 的分数的数量
]VY0TQ
/// </summary>
Oe1|9Vq3h(iq988 /// <returns></returns>
public int? ScoresQueryCount()
{
int[] scores = new int[] { 88, 79, 61, 59, 74, 58, 49, 97, 99, 84 };
if (scores.Length != 0)
Ta M&]
i!K#XR:H
{
//var scoresQueryCount = (from score in scores where score > 70 select score).Count();//查询语法
var scoresQueryCount = scores.Where(score => score > 70).Count();//Lambda表达式(方法语法)
z?uH-g.n
Console.WriteLine("下面数据是计算得到数据的count");
Console.WriteLine(scoresQueryCount);
Console.WriteLine("数据输出已完成!");
return 0;
}
else
{
return null;
|V9m5L4ekr*h4@Q
}
}
}
}