码迷,mamicode.com
首页 > Windows程序 > 详细

C# LINQ 中关于from和select的一个小例子

时间:2015-06-02 14:48:48      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

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);
            }
            
           
        }
    }
}

 

C# LINQ 中关于from和select的一个小例子

标签:

原文地址:http://www.cnblogs.com/crandy/p/4546126.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!