标签:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace First_exam { class Student { /// <summary> ///学生姓名的字段和属性 /// </summary> private string _Name; public string Name { get { return this._Name; } } private string _Xingbie; /// <summary> /// 学生性别的字段和属性 /// </summary> public string Xingbie { get { return this._Xingbie; } } /// <summary> /// 学生年龄的字段和属性 /// </summary> private uint _Age; public uint Age { get { return this._Age; } } /// <summary> /// 学生成绩的字段和属性 /// </summary> private List<LessonScore> _Scores; public List<LessonScore> Scores { get { return this._Scores; } } /// <summary> /// 构造函数,传入学生姓名,性别,年龄,成绩 /// </summary> public Student(string name, string xb, uint age, List<LessonScore> scrs) { this._Name = name; this._Xingbie = xb; this._Age = age; this._Scores = scrs; } public Student(string name, string xb, uint age) { this._Name = name; this._Xingbie = xb; this._Age = age; this._Scores = null; } public override string ToString() { string str; str = string.Format("{0}--{1}--{2}",this._Name, this._Age,this._Xingbie); return str; } } class LessonScore { /// <summary> /// 课程成绩的字段和属性 /// </summary> private float _Score; public float Score { get { return this._Score; } } /// <summary> /// 课程名称的字段和属性 /// </summary> private string _Lessson; public string Lesson { get { return this._Lessson; } } /// <summary> /// 构造函数 /// </summary> public LessonScore(string les, float scr) { this._Lessson = les; this._Score = scr; } public override string ToString() { string str; str = string.Format("{0}----{1}分", this._Lessson,this._Score); return str; } } class Program { static void Main(string[] args) { Student[] arr = { new Student("张氏那","男",20), new Student("李四","男",23), new Student("李霞","女",21), new Student("王妈妈","女",29), new Student("郭明","男",22), new Student("欧阳夏","女",24), new Student("王丹","女",20), new Student("张宝","男",25), }; var query = from val in arr select val; foreach (Student item in query) { Console.WriteLine(item); } Console.WriteLine(); var query2 = from val in arr select val.Name; foreach(string item in query2) { Console.Write("{0}, ",item); } Console.WriteLine(); Console.WriteLine(); var query3 = from val in arr select val.Name.Length; foreach (int item in query3) { Console.Write("{0}, ", item); } Console.WriteLine(); Console.WriteLine(); var query4 = from val in arr select new { val.Name, val.Age, NameLen = val.Name.Length}; foreach(var item2 in query4) { Console.WriteLine(item2); } } } }
标签:
原文地址:http://www.cnblogs.com/crandy/p/4546126.html