EXAMPLE FOR LINQ TO OBJECT
10983 ワード
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace LINQ
- {
- class student
- {
- private string name;
-
- public string MyName
- {
- get { return name; }
- set { name = value; }
- }
-
- private string sex;
-
- public string MySex
- {
- get { return sex; }
- set { sex = value; }
- }
- private int age;
-
- public int MyAge
- {
- get { return age; }
- set { age = value; }
- }
-
- private string schoolname;
-
- public string MySchoolName
- {
- get { return schoolname; }
- set { schoolname = value; }
- }
-
- private string classname;
-
- public string MyClassName
- {
- get { return classname; }
- set { classname = value; }
- }
-
-
- }
- class schoolinfo
- {
- private string schoolname;
-
- public string MySchoolName
- {
- get { return schoolname; }
- set { schoolname = value; }
- }
-
- private string classname;
-
- public string MyClassName
- {
- get { return classname; }
- set { schoolname = value; }
- }
-
-
- }
- class Program
- {
- public delegate void ShowSudentMessage();
- static void Main(string[] args)
- {
-
- List<student> students = new List<student>()
- {
- new student{MyName = "zhangsan",MyAge = 21, MyClassName = "CS-03",MySchoolName = "NCUT",MySex = "MALE"},
- new student{MyName = "lixi",MyAge = 22, MyClassName = "CS-03",MySchoolName = "NCUT",MySex = "MALE"},
- new student{MyName = "wangwu",MyAge = 23, MyClassName = "CS-03",MySchoolName = "NCUT",MySex = "MALE"}
- };
- var schoolinfo = from p in students
- where p.MyAge < 30
- select new
- {
- SchoolName = p.MySchoolName,
- ClassName = p.MyClassName
- };
-
- ShowMessage(() =>
- {
- foreach (var item in schoolinfo)
- {
- Console.WriteLine(item.SchoolName + " " + item.ClassName);
- }
- });
- Console.ReadKey();
- }
- static void ShowMessage(ShowSudentMessage ExecuteShow)
- {
- ExecuteShow();
- }
- }
- }