/// <summary>
/// Linq查询语句,ary表示数据源,val表示本地变量,可以对ary进行指定类型
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
//实例一
//int[] ary = { 1, 2, 5, 4, 7, 5, 9 };
//var query1=from val in ary select val;/// Linq查询语句,ary表示数据源,val表示本地变量,可以对ary进行指定类型
//foreach (var item in query1)
//{
// Console.WriteLine("{0} ", item);
//}
//实例二
Student[] stAry =
{
new Student("张三","男",20),
new Student("高松","男",20),
new Student("你妹","男",20),
new Student("你姐","男",20),
};
var query1 = from vall in stAry select vall;
var query2 = from vall in stAry select vall.Name;
var query3 = from vall in stAry select vall.Name.Length;
foreach (Student item in query1)
{
Console.WriteLine(item);
}
foreach (string item in query2)
{
Console.WriteLine(item);
}
foreach (int item in query3)
{
Console.WriteLine(item);
}
var query4 = from vall in stAry select new { vall.Name, vall.Age, NameLen = vall.Name.Length };//匿名类型方式输出
foreach (var item in query4)
{
Console.WriteLine(item);
}
}
class LessonScore
{
/// <summary>
/// 创建成绩单,传入课程名,分数
/// </summary>
/// <param name="_les"></param>
/// <param name="scr"></param>
public LessonScore(string _les, float _scr)
{
this.Lesson = _les;
this.Score = _scr;
}
public float Score { get; set; }
public string Lesson { get; set; }
public override string ToString()
{
string str;
str = string.Format("{0}-----------{1}", this.Lesson, this.Score);
return str;
}
}
class Student
{
/// <summary>
/// 学生类
/// </summary>
public string Name { get; set; }
public string Sex { get; set; }
public uint Age{get;set;}
public List<LessonScore> StuScores { get; set; }
public Student(string _name, string _sex, uint _age, List<LessonScore> _scrs)
{
this.Name = _name;
this.Sex = _sex;
this.Age = _age;
this.StuScores = _scrs;
}
public Student(string _name, string _sex, uint _age)
{
this.Name = _name;
this.Sex = _sex;
this.Age = _age;
this.StuScores = null;
}
public override string ToString()
{
string str;
str = string.Format("{0}--{1}--{2}", this.Name, this.Age, this.Sex);
return str;
}
}