LINQ学习笔记1

     LINQ是微软在.NET Framework3.5框架上推出的,传统的针对数据的查询是编写字符串文本来写的查询语句,这种方法常常没有编译时的类型检查,安全性、方便性不太好,而LINQ则使需要查询的数据源包含一组数据的集合对象(IEnumerable<T>或IQeryable<T>)类型,返回的查询结果也是一组数据的集合对象。也就是说使用数据查询可以像操作对象一样。

   LINQ提供两种方式查询数据,一种是查询表达式,这有点像SQL语句,还有一种是查询方法,因为数据源和查询结果都是IEnumerable<T>或IQeryable<T>类型对象,所以可以使用普通对象的形式如调用方法、使用属性等方式来对数据源进行查询,查询方法可以用Lambda表达式。

  下面看一个使用查询表达式查询数据的例子:

    public class LessonScore
    {
        //字段
        private string _Lesson;
        private float _Score;

        //属性
        public string Lesson
        {
            get { return _Lesson; }
        }
        public float Score
        {
            get { return _Score; }
        }

        public LessonScore(string lesson, float score)
        {
            this._Lesson = lesson;
            this._Score = score;
        }

        //重写ToString方法,按照特定格式打印成绩信息
        public override string ToString()
        {
            string str;
            str = string.Format("{0}---{1}分", this._Lesson, this._Score);
            return str;
        }
    }
    public class Student
    {
        //字段
        private string _Name;
        private uint _Age;
        private string _XingBie;
        private List<LessonScore> _Scores;
        //属性
        public string Name
        {
            get { return _Name; }
        }
        public uint Age
        {
            get { return _Age; }
        }
        public string XingBie
        {
            get { return _XingBie; }
        }
        public List<LessonScore> Scores
        {
            get
            {
                return this._Scores;
            }
        }
        //构造函数
        public Student(string name, uint age, string xingbie, List<LessonScore> scores)
        {
            this._Name = name;
            this._Age = age;
            this._XingBie = xingbie;
            this._Scores = scores;
        }
        //重写ToString()方法,获取学生信息
        public override string ToString()
        {
            string str;
            str = string.Format("{0}-{1}-{2}", this._Name, this._Age, this._XingBie);
            return str;
        }
    }

主方法体:

 static void Main(string[] args)
        {
            Student[] array = new Student[]
            {
                new Student("张飞",20,"",
                    new List<LessonScore>{ new LessonScore("数学",70.0f),new LessonScore("语文",60.5f),new LessonScore("英语",80.5f)}),
                new Student("貂蝉",18,"",
                    new List<LessonScore>{ new LessonScore("数学",73.5f),new LessonScore("语文",88.5f),new LessonScore("英语",69.0f)}),
                new Student("吕布",31,"",
                    new List<LessonScore>{ new LessonScore("数学",39.5f),new LessonScore("语文",55.0f),new LessonScore("英语",30.5f)}),
                new Student("关羽",25,"",
                    new List<LessonScore>{ new LessonScore("数学",78.0f),new LessonScore("语文",78.0f),new LessonScore("英语",28.5f)}),
                new Student("夏侯惠",7,"",
                    new List<LessonScore>{ new LessonScore("数学",82.0f),new LessonScore("语文",93.5f),new LessonScore("英语",79.0f)})
            };
            var query1 = from st in array
                         from sco in st.Scores
                         where sco.Score > 80
                         group sco by st.Name;
            foreach (var a in query1)
            {
                System.Console.WriteLine(a.Key);
                foreach (var item in a)
                {
                    System.Console.WriteLine("\t{0}", item);
                }
            }
        }

  其中Student对象中有一个字段为List<LessonScore>对象类型,表明为一对多的关系,此查询表达式根据数据源中的姓名分组查询出分数大于80的课程信息,按照LessonScore类中重写的ToString()方法打印出来。执行结果如下:

  查询方法中提供了很多函数,如GroupBy()、Where()等,就像调用对象的方法一样使用就行了,这里就不介绍了。
  第一次写总结,都不知怎么构思,让大家见笑了,以后多写写,慢慢提高吧。
posted @ 2012-11-16 11:09  Obsueter  阅读(460)  评论(0编辑  收藏  举报